dlang / project-ideas

Collection of impactful projects in the D ecosystem
36 stars 12 forks source link

Implementing Parent Enum Inference in the language (.MyValue instead of MyEnum.MyValue) #88

Open ryuukk opened 2 years ago

ryuukk commented 2 years ago

Description

How many times have you been tired of repeating yourself when using enums?

set_my_flag( MySuperLongName.MyFlagA |  MySuperLongName.MyFlagB | MySuperLongName.MyFlagC | MySuperLongName.MyFlagD | MySuperLongName.MyFlagE | MySuperLongName.MyFlagF );

What if instead the compiler could be smarter and just accept this:

set_my_flag( .MyFlagA |  .MyFlagB | .MyFlagC | .MyFlagD | .MyFlagE | .MyFlagF );

The result already speaks for itself, no?

There is the with() we can use, but unfortunately, it doesn't really help with the repetition, and you can't use it everywhere, plus it leaks the the entire scope

Many other new languages went with that: (Swift, Zig, Odin, Jail, Styx)

What are rough milestones of this project?

A PoC that could help kickstart a proper DIP

How does this project help the D community?

It helps when using enums a lot and prevents us from choosing shorter names only because they are too long to use

Also it helps avoid repetition, wich doesn't help with visual clarity

Recommended skills

Basics of language/compiler development

Perfect for GSoC/SAoC

What can students expect to get out of doing this project?

They can learn about working and contributing to a big project (compiler)

And potentially adding a new feature to it!

Point of Contact

You can contact me if you have questions, leave a message here, or we can share emails if you prefer, just le me know!

References

https://forum.dlang.org/thread/yxxhemcpfkdwewvzulxf@forum.dlang.org

How it was implemented in Styx, and how it could be done in DMD:

https://forum.dlang.org/post/kkbrjhokqctzjupleqbq@forum.dlang.org

mdparker commented 2 years ago

@ryuukk FYI, the "what can students expect to get..." isn't about money, but about the kind of practical experience they can expect to gain. E.g., in this case, learning about the compiler internals, how to contribute to the compiler, etc.

And the Point of Contact isn't about where to discuss this generally. It's who to contact for more information about the idea. This project is your idea, so in this case you're the obvious point of contact for anyone who has questions (and this is the obvious place to ask questions, so you don't need to list anything there at all). It's fine to list the forums rather than your email address, but please note anything they should add to the subject line of a forum post that would get your attention should they want to ask about it. This goes for your other project idea as well.

ryuukk commented 2 years ago

@ryuukk FYI, the "what can students expect to get..." isn't about money, but about the kind of practical experience they can expect to gain. E.g., in this case, learning about the compiler internals, how to contribute to the compiler, etc.

And the Point of Contact isn't about where to discuss this generally. It's who to contact for more information about the idea. This project is your idea, so in this case you're the obvious point of contact for anyone who has questions (and this is the obvious place to ask questions, so you don't need to list anything there at all). It's fine to list the forums rather than your email address, but please note anything they should add to the subject line of a forum post that would get your attention should they want to ask about it. This goes for your other project idea as well.

Thanks a lot for your reply, i applied your recommendations and edited the post, i will do that as well for the other one

ichordev commented 1 year ago

I've written a DIP for this since it's something I've wanted in the language for quite some time. https://github.com/dlang/DIPs/pull/230

ryuukk commented 1 year ago

@ichordev

Thanks a lot for the DIP!

I also wanted that feature for a very long time, hopefully we'll manage to make it happen!

ichordev commented 1 year ago

@ichordev

Thanks a lot for the DIP!

I also wanted that feature for a very long time, hopefully we'll manage to make it happen!

Not a problem! I’ve felt that it would be a really worthwhile shortcut ever since I started using lots of enums in some of my Dlang projects. :)

ntrel commented 1 year ago

Just to mention this feature is also useful with Walter's sum type proposal:

sumtype S { int val; string name; }
S s;
...
s = .val(5); // infer type