September 07, 2003 Re: Newbie question - templates v dynamic subclass casting | ||||
---|---|---|---|---|
| ||||
Posted in reply to BDP | BDP wrote: > > Again, please excuse my ignorance, but if you have a template, how would > you check for things like > > in > { > assert(object!=null); } > > This code snippet is valid for class references but not primitive types. D allows templates to be specified for use with a heirachy of class/types. i.e. template mine( T : Object ) { void func( T object ) in { assert( object!== null ); } // note the "! = =" // for object/ref types // D "= = =" is eqiv to java "= =" // D "a = = b" is eqiv to Java a.equals( b ) // D "a ! = b" is eqiv to Java !a.equals(b) // D "! = = " is eqiv to Java "!=" } template mine( T : int|uint|short|ushort ) { void func( T object ) in { assert( object != 0 ); } // note the "! = =" } you can't make an instance of "mine" with struct or floating point type. problem solved, (the Generics in Java spec allows this too) > > It seems to me that you pay a price for generality in terms of strong > program type checking and contract programming. > I disagree dynamically typed and weakly typed langs handle can template in some cases better than strongly typed langs. everything has a price, in Java you have to cast in any "container" class. however with D the price is mainly in the source rather than the object files. |
Copyright © 1999-2021 by the D Language Foundation