Why can't I have overloading and generics?

so so at so.so
Sat Mar 10 01:47:14 PST 2012


On Saturday, 10 March 2012 at 03:32:44 UTC, Caligo wrote:
>     struct B { }
>     struct C { }
>     struct D { }
>
>     struct A {
>
>       ref A foo(B item) {
>         /* do something special. */
>         return this;
>       }
>
>       ref A foo(T)(T item) if(is(T == C) || is(T == D)) {
>         /* nothing special, do the same for C and D. */
>         return this;
>       }
>     }
>
> Is this unreasonable?  iirc, C++ supports this, but not D.  
> What's the
> reason? Bug?
>
> What's a good solution to this?
>
> 1. a generic `foo()` that uses `static if`s?
>
> 2. overload `foo()`, even if it means having function bodies 
> that are
> exactly same (code duplication).?
>
> 3. mixin templates?  I don't know about this because TDPL says 
> it's
> experimental, and I've tried and I get weird errors.

When it comes to templates if it works in C++ and not in D, you 
can be sure it is a bug OR future!

For this one i am sure it is a bug. Rule is, when language 
resolves the function it first looks for exact matches then 
template overloads and i can't see anything wrong in your code. I 
am not sure but it is probably about "is". Its usage looks pretty 
in your code, it should not! :)


More information about the Digitalmars-d-learn mailing list