Template issues in D

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Thu Feb 21 07:30:08 PST 2008


Burton Radons wrote:

> I'm not so sure I want the language to act differently just for templates.
> What I would like is for specialisations to be predicatable. That would
> not only give us the power of extensible templates, but it would allow new
> features as well. So I would use:
> 
> // Or whatever you'd use to get the values of an array.
> T add_arrays (T = is_array! (T) && is_numeric! (array_values! (T))) (T a,
> T b) {
> }
> 
> I would be very happy with that, and the compiler could specially
> understand that so might fail it with "array_add cannot be used with foo
> because it failed the is_array predicate."

Maybe you want something like type classes
(http://en.wikipedia.org/wiki/Type_class). The code above could look
something like (another more D style alternative is c++0x concepts, I
think, but I can't remember their syntax):

  add_arrays:: [T] [T] -> [T] | Numeric T
  add_arrays a b = ...

I'm not sure if the current syntax can be elegantly abused to accept these.
A dedicated syntax would be much easier to read and would work in cases
where you want bounded polymorphism with predicates.



More information about the Digitalmars-d mailing list