A Modest Proposal: Final class instances

Stewart Gordon smjg_1998 at yahoo.com
Tue May 5 17:01:24 PDT 2009


dsimcha wrote:
<snip>
> 1.  Method calls don't need to be virtual.
> 2.  An instance of a subclass cannot be converted to a final instance of the
> base class.
> 3.  A final instance can be implicitly converted to a non-final instance, but
> the opposite would not work.
> 
> Using final as an instance attribute like this would also allow another useful
> feature:  Storing class instances inline in arrays, structs, or other classes.
>  Basically, by marking a class instance as final, you'd be telling the
> compiler that you do not need and are not using polymorphism in this case,
> even if the class hierarchy uses it for other use cases, and therefore, all
> relevant optimizations can be made.

So the class would be contained by value, and method calls would bypass 
the vtable.  Effectively, the class would become more like a struct in 
these instances.

There are two difficulties I can see:

- either the final instance would still need a vtable to support methods 
calling each other, or there'd have to be internally two versions of 
each method
- containment by value means that one would have to deal with copying of 
instances, in ways that you wouldn't have to worry about if you were 
using the same class normally.

Stewart.



More information about the Digitalmars-d mailing list