Enum literals, good? bad? what do you think?
John Colvin
john.loughran.colvin at gmail.com
Thu Jul 22 16:59:49 UTC 2021
On Thursday, 22 July 2021 at 16:41:30 UTC, Paul Backus wrote:
>
> IMO D's target-typed literals (also called "polysemous
> literals") are also a misfeature. They lead to weird,
> unintuitive situations where seemingly-equivalent code has
> different behavior; for example:
>
> ```d
> import std.stdio;
>
> void fun(double[] arr) { writeln("double"); }
> void fun(T)(T[] arr) { writeln(T.stringof); }
>
> void main()
> {
> fun([1, 2, 3]);
> auto arr = [1, 2, 3];
> fun(arr);
> }
> ```
>
> A language with fully-context-aware type inference would infer
> both arrays as `double[]`. A language with
> fully-context-independent type inference would infer both as
> `int[]`.
>
> In D, however, we get the worst of both worlds: type inference
> is *mostly* context-independent (so we have to add type
> annotations even in cases that are unambiguous), but there are
> a few special cases where context is taken into account (so we
> have to be mindful of context when refactoring, since a change
> in context could change the inferred type of an expression).
It’s not so bad IMO. Type inference in D isn’t far from just
“forward, one step at a time”, which is easy to understand but
still very powerful.
More information about the Digitalmars-d
mailing list