PROPOSAL: Improved Support for Optional Object Behaviors at Runtime

Mark Thornton mthornton at optrak.co.uk
Thu Mar 26 12:25:27 PDT 2009


rssh at gradsoft.com.ua wrote:
>> EXAMPLE:
>>
>> Suppose a program wants to test an object "o" at runtime for an optional
>> behavior defined by a class or interface "T". In current Java, the program
>> could write:
>>
>> 	try {
>> 		T t = (T) o;
>> 		... use t ...
>> 	} catch (ClassCastException ex) {
>> 	}
>>
>> Using the proposed feature, the program would write:
>>
>> 	T t = o.getExtension(T.class);
>> 	if (t != null) {
>> 		... use t ...
>> 	}
>>
>> The following examples are all hypothetical, but plausible to varying
>> degrees. Note that many of them use instances of existing platform
>>     
>
>
> Sorry, I can't understand, how this differ from
>
> if (o instanceof T) {
>   T t = (T)o;
>     .... use t ...
> }
>
> ?
>
>
>
>   
The getExtension method need not return its own object but could 
delegate to another object. This functionality is essentially the same 
as that provided by objects implementing the java.sql.Wrapper interface.

Mark Thornton




More information about the coin-dev mailing list