Discussion Thread: DIP 1044--Enum Type Inference--Community Review Round 1

Timon Gehr timon.gehr at gmx.ch
Fri Nov 18 22:49:33 UTC 2022


On 18.11.22 22:09, Walter Bright wrote:
> What happens in combination with function and template overloading is not discussed.

It does not have to be.

> For example:
> 
>     enum A { a }
>     enum B { a }
> 
>     void ket(A);
>     void ket(B);
> 
>     ...
>     ket(a);
>     ...
> ...

Should be ket($a).

> That's the simplest case. More complex cases come when there are multiple overloaded functions with diverse enum arguments, resulting in an unbounded combinatorial problem of which combination of enum members will be selected.
> ...

This is just not true. Overloading introduces zero additional problems. 
D already has well-defined overloading rules. You only have to be able 
to match each function on its own.


> This kind of problem comes up whenever we contemplate adding top-down type inference and try to make it work in combination with the current bottom up method. It works in trivial cases as shown in the DIP, but the complex ones are the problem.

Why "contemplate"? D already has such inference for delegates.

```d
import std.stdio;

enum A{
     a,
     b,
}
enum B{
     b,
     c,
}

void foo(A delegate(A) dg){
     writeln("first overload");
}
void foo(B delegate(B) dg){
     writeln("second overload");
}

void main(){
     foo((x){ // first overload
         static if(__traits(hasMember,typeof(x),"a"))
             return typeof(x).a;
     });
     /+foo((x){ // error, ambiguous
         static if(__traits(hasMember,typeof(x),"b"))
             return typeof(x).b;
     });+/
     foo((x){ // second overload
         static if(__traits(hasMember,typeof(x),"c"))
             return typeof(x).c;
     });
}
```

It works just fine. Now just do literally the same thing for the new 
enum inference. Probably one can even reuse some of the compiler code.

> (Even worse than the function overloading problem is the template overloading problem, as the compiler will need to instantiate the template with each combination of enum inferences just to figure out what the type of the template is.) 

Again, this has nothing to do with overloading. In any case, just do the 
same as for the delegate case and reject if there is no explicitly given 
enum type to pick up on in the function signature. Instantiating all 
combinations is never what the user expects.


More information about the Digitalmars-d mailing list