Discussion Thread: DIP 1044--Enum Type Inference--Community Review Round 1
Dukc
ajieskola at gmail.com
Sat Nov 19 08:25:12 UTC 2022
On Friday, 18 November 2022 at 22:49:33 UTC, Timon Gehr wrote:
> 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.
Excellent observation, definitely did not occur to me! IMO this
would greatly increase the appeal of this DIP. It can sidestep
many of the difficult questions about corner cases with complex
expressions.
More information about the Digitalmars-d
mailing list