Type Inference for Struct/Enum Literals
IchorDev
zxinsworld at gmail.com
Tue Jul 9 03:51:57 UTC 2024
On Monday, 8 July 2024 at 10:41:39 UTC, Nick Treleaven wrote:
> Vladimir's idea is for an identifier to have its own type. When
> omitting the struct type, there is no identifier. So that idea
> doesn't have a bearing on being able to do that.
> ```d
> MyStruct x;
> x = _(args); // type identifier not present
> ```
I see what you mean, but `_` is actually an identifier. ;) I know
a lot of us would like to deprecate it favour of pattern matching
discards/etc., but it would still be a considerable syntax break.
To solve the issue you mentioned about there being no identifier,
instead of having a type for an identifier, we make another
similar type that stores just a list of parameters.
> BTW it would be nice to be able to omit the class type in a
> NewExpression too:
> ```d
> MyClass obj;
> obj = new _(args);
> ```
Absolutely, that's a great idea! Now, this presents an
interesting question:
Can `new`+type inference only be used for classes, or can `new
MyStruct(args)` also be shortened to `new _(args)`? Personally I
think allowing structs makes sense.
Class type inference would certainly grant a bit less benefit
than for other aggregates due to polymorphism, but as long it's
doable I'm down for it. I guess I should mention that I think
union literals should get type inference too before someone
brings those up. One really cool way of inferring their type
would be via named parameters.
More information about the dip.ideas
mailing list