digitalmars.D - pay-as-you-go template or constraint genericity?
- no where.com (25/25) Nov 04 2004 I'm not sure if this issue has been discussed before: I just encoutered...
- Sean Kelly (8/28) Nov 05 2004 I'm not sure if it is feasible either, but it would be very nice to have...
I'm not sure if this issue has been discussed before: I just encoutered the following compiler error message class Foo {...} Vector!(Foo) fv; F:\dlib\std\dtl\containers\vector.d(620): no property 'min' for type 'Foo' and the offending line in file vector.d are // This will need to be genericised in order to work with // reference types and scalar types return value_type.min; I wonder how should we avoid such issues. 1) C++'s pay-as-you-go approach: in my case, I don't need the feature Vector!(Foo).min() at all. So this function should not be instanciated by the compiler at all. I'm not sure how template is implmenented in D, and if this approach is feasiable. 2) Move .min() out of the template definition, and define it as a global function min(Vector ...) maybe in a different module. Still this require the compiler to instantiate the function only when it's necessary by calculating the call-graph. 3) the compiler always blindly instantiates everything; then in this case the library designer must take care not to write code that refer to properties that are not avaiable in Object and scalar types. 4) constraint genericity as in Eiffel and Java 1.5: define template togather with the type which is allowed to be passed in as type arguments: Vector( T: Minable ). So only types which defined .min() can be passed to Vector. So which approach is the current D taking now?
Nov 04 2004
In article <cmf4te$v16$1 digitaldaemon.com>, no where.com says...I'm not sure if this issue has been discussed before: I just encoutered the following compiler error message class Foo {...} Vector!(Foo) fv; F:\dlib\std\dtl\containers\vector.d(620): no property 'min' for type 'Foo' and the offending line in file vector.d are // This will need to be genericised in order to work with // reference types and scalar types return value_type.min; I wonder how should we avoid such issues. 1) C++'s pay-as-you-go approach: in my case, I don't need the feature Vector!(Foo).min() at all. So this function should not be instanciated by the compiler at all. I'm not sure how template is implmenented in D, and if this approach is feasiable.I'm not sure if it is feasible either, but it would be very nice to have. Convenience aside, it would also result in a smaller code footprint, which would be advantageous for a systems programming language.3) the compiler always blindly instantiates everything; then in this case the library designer must take care not to write code that refer to properties that are not avaiable in Object and scalar types.4) constraint genericity as in Eiffel and Java 1.5: define template togather with the type which is allowed to be passed in as type arguments: Vector( T: Minable ). So only types which defined .min() can be passed to Vector.This should certainly be possible using traits templates, but it should not be necessary. Sean
Nov 05 2004