Why D functions paramter can not implicit infer type of Variant?

sighoya sighoya at gmail.com
Wed Jan 13 18:09:08 UTC 2021


On Wednesday, 13 January 2021 at 16:17:02 UTC, Marcone wrote:
> import std;
>
> void a(int b){
> }
>
> void main()
> {
>   Variant c = 10;
>   a(c); // Error
> }
>
> Need more sugar.

Two problems:

1.) Variant is library defined, compared to the language level 
there isn't a default strategy to choose int32 here, it could be 
also, short, long, unsigned ... not to mention all the alias this 
types.
Though it may be possible to define a default strategy but part 
of the problem is how to tell D to init the type parameters 
appropriately.

2.) c is mutable, what is if you return c and assign other values 
of other types to it?
Determining all possibilities by traversing following assignments 
leads to global type inference which no one would ever want to 
have especially with support of subtyping.

A more natural conclusion would be to infer c to the most common 
supertype as other inferences would unnecessarily exclude future 
assignments to c.  But the most common supertype doesn't seem to 
exist, and I'm unsure if this type can be modeled at all in D?




More information about the Digitalmars-d-learn mailing list