July 21, 2021

On Wednesday, 21 July 2021 at 13:36:37 UTC, Steven Schveighoffer wrote:

>

On 7/20/21 11:50 AM, russhy wrote:

>

Hello

I all the time wondered why we always have to be so much verbose with enum, when it's not casting to primitives, it is about repeating their long type name, constantly, all the time

After trying some other languages over the past few years, i discovered in zig you can just ommit the enum type name and just use enums this way: .MY_VALUE

I don't know if that's something that could be supported with D, i am not a compiler dude, so i don't have the answer

Adam on discord mentioned using with(ENUM_TYPE) or just an alias, but i think we go ahead and make it simple

I had prepared a DIP [1], not ready at all, but i wanted to initiate some discussion about that feature

So what do you think? yay? nay? why not?

[1] https://github.com/RUSshy/DIPs/blob/patch-2/DIPs/DIP1xxx.md

I enjoy having this type of shortcut in Swift, and wish D had something similar.

To give some perspective, look no further than configuration options of std.algorithm:

arr.sort!("a < b", .unstable); // instead of SwapStrategy.unstable

Clear as the original, and less verbose. In Swift, enums are used extensively, and I don't think they would be if the shortcut syntax wasn't available.

And in general, any function parameter enums are cumbersome to write in D.

However, using .enumMember isn't viable as noted by several people.

If I were to propose something, it would be to have a short substitute for the type. Something like #.typeMember which then uses the expression type to lookup the member. Then you can do things like #.init or any other type properties that return an instance of the type. It wouldn't be a panacea though, since overloading would make this ambiguous. But assignment/initialization would be unambiguous.

Probably not going to fly in D.

-Steve
of course the . syntax isn't final, the point of the DIP discussion was to discuss about the ability to omit the enum type name, the syntax can always be something else

July 21, 2021

of course the . syntax isn't final, the point of the DIP discussion was to discuss about the ability to omit the enum type name, the syntax can always be something else

edit: quoting is kind hard to follow in the text editor here, i made a mistake on the reply above by typing inside the quote

July 21, 2021

On Wednesday, 21 July 2021 at 06:12:20 UTC, zjh wrote:

>

On Tuesday, 20 July 2021 at 15:50:49 UTC, russhy wrote:

>

Hello

And the default constructor,
Why can't it like C++: T t; That's all.
Many C++ programmers want to have constructors like C++ as T t{...};.

yes that too.. if you can create DIP draft for it and post it on the forum too? se we can have discussion about that too

July 21, 2021
On 7/21/2021 12:11 AM, Patrick Schluter wrote:
>> `alias` is an all-purpose tool for moving names from one scope to another. Of course, one can probably do a mixin to automate the alias declarations. It should be a fun exercise. Any takers?
> 
> mixin({
>    auto aliasList = "";
>    static foreach(m; __traits(allMembers, MyEnum))
>      aliasList ~= "alias "~m~" = MyEnum."~m~";\n"
>    return aliasList;
> }());

Sweet!

Please put in the D wiki somewhere!
July 21, 2021
On 7/21/2021 12:12 AM, Mike Parker wrote:
> enum expandEnum(EnumType, string fqnEnumType = EnumType.stringof) = (){
>      string expandEnum = "enum {";
>      foreach(m;__traits(allMembers, EnumType)) {
>          expandEnum ~= m ~ " = " ~ fqnEnumType ~ "." ~ m ~ ",";
>      }
>      expandEnum  ~= "}";
>      return expandEnum;
> }();
> 
> enum MyEnum { blah, bleh, bluh }
> mixin(expandEnum!MyEnum);
> 
> auto e = blah;


You & Patrick are da men!

This would make a nice bite-sized article showing off D's metaprogramming facilities.
July 21, 2021
On 7/21/2021 6:36 AM, Steven Schveighoffer wrote:
> I enjoy having this type of shortcut in Swift, and wish D had something similar.

Your wish has been heard and duly granted. The internet delivers once again!

See Patrick Schluter and Mike Parker's solutions upthread.
July 21, 2021
On 7/21/2021 12:08 PM, russhy wrote:
> On Wednesday, 21 July 2021 at 06:12:20 UTC, zjh wrote:
>> On Tuesday, 20 July 2021 at 15:50:49 UTC, russhy wrote:
>>> Hello
>>
>>
>> And the default constructor,
>> Why can't it like `C++`: `T t;` That's all.
>> Many C++ programmers want to have constructors like `C++` as `T t{...};`.
> 
> yes that too.. if you can create DIP draft for it and post it on the forum too? se we can have discussion about that too

It's been discussed many times. It comes up once a year or so.
July 21, 2021

On 7/21/21 5:02 PM, Walter Bright wrote:

>

On 7/21/2021 6:36 AM, Steven Schveighoffer wrote:

>

I enjoy having this type of shortcut in Swift, and wish D had something similar.

Your wish has been heard and duly granted. The internet delivers once again!

See Patrick Schluter and Mike Parker's solutions upthread.

No, mixin something; thenCallFunction(...); isn't even close to the same.

-Steve

July 21, 2021
On Wednesday, 21 July 2021 at 07:12:25 UTC, Mike Parker wrote:
> ```d
> enum expandEnum(EnumType, string fqnEnumType = EnumType.stringof) = (){

Eeek, this is awful. It uses .stringof which means it is automatically stinky. It is liable for trouble when anything other than basic imports get involved.

If you are going to do this kind of thing as a mixin at all, a mixin template is far more appropriate. It avoids the scope trouble since it can use the unambiguous local name.

But when there's `with`.... none of this stuff gains you anything. It doesn't get closer to the Swift examples.
July 21, 2021
On 7/21/2021 3:22 PM, Adam D Ruppe wrote:
> On Wednesday, 21 July 2021 at 07:12:25 UTC, Mike Parker wrote:
>> ```d
>> enum expandEnum(EnumType, string fqnEnumType = EnumType.stringof) = (){
> 
> Eeek, this is awful. It uses .stringof which means it is automatically stinky. It is liable for trouble when anything other than basic imports get involved.

Just use it right after the declaration of the enum.

> It doesn't get closer to the Swift examples.

How so?