Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
January 06, 2018 Alias Vs. Enum? | ||||
---|---|---|---|---|
| ||||
Is there a reason for the differences between Enum and Alias? For the most part enums are only used for things that have a value, but alias is used more for types. But with templates you can get around this and you basically get the funcitonality of Enum for alias. Oddly enough from the template parameter being "alias". template valueOf(alias v) { } alias aa = AliasSeq!(10 == 10); enum ee = 10 == 10; alias err = 10 == 10; // error What are the actually differences here with aa and ee? |
January 06, 2018 Re: Alias Vs. Enum? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rubn | Tab + Enter + No Delete/Edit = :/ template valueOf(alias v) // <-- alias { alias valueOf = v; } alias aa = valueOf!(10 == 10); enum ee = 10 == 10; alias err = 10 == 10; // error Can't alias just be extended to support enum values as well instead of having this workaround with templates? Is there any reason this hasn't already been done? |
January 07, 2018 Re: Alias Vs. Enum? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rubn | On 06.01.2018 22:36, Rubn wrote: > Tab + Enter + No Delete/Edit = :/ > > > template valueOf(alias v) // <-- alias > { > alias valueOf = v; > } > > alias aa = valueOf!(10 == 10); > enum ee = 10 == 10; > > alias err = 10 == 10; // error > > > Can't alias just be extended to support enum values as well instead of having this workaround with templates? Yes, it can. > Is there any reason this hasn't already been done? > > > Nobody stepped up and did it, I guess. I might write a DIP to clean up the language grammar at some point. (There are a few more cases like this one.) |
January 07, 2018 Re: Alias Vs. Enum? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rubn | On Saturday, 6 January 2018 at 21:33:46 UTC, Rubn wrote:
> Is there a reason for the differences between Enum and Alias? For the most part enums are only used for things that have a value, but alias is used more for types. But with templates you can get around this and you basically get the funcitonality of Enum for alias. Oddly enough from the template parameter being "alias".
>
>
> template valueOf(alias v)
> {
>
> }
>
> alias aa = AliasSeq!(10 == 10);
> enum ee = 10 == 10;
>
> alias err = 10 == 10; // error
>
> What are the actually differences here with aa and ee?
The compiler can only alias to symbols and not to values.
therefore enum was chosen for manifest constants.
That alias can bind to values in template-parameters is useful but not exactly consistent :)
|
January 07, 2018 Re: Alias Vs. Enum? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | On Sunday, 7 January 2018 at 03:52:53 UTC, Stefan Koch wrote:
> The compiler can only alias to symbols and not to values.
> therefore enum was chosen for manifest constants.
>
> That alias can bind to values in template-parameters is useful but not exactly consistent :)
Not only that, but alias can bind to values if they're from a template alias parameter, hence std.meta.Alias. Instead of doing that silly dance, alias should simply take values as well.
--
Simen
|
January 07, 2018 Re: Alias Vs. Enum? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjærås | On Sunday, 7 January 2018 at 18:30:17 UTC, Simen Kjærås wrote:
> Instead of doing that silly dance, alias should simply take values as well.
Also, using "enum" for manifest constants makes sense for people familiar with C idiom, but often confuses people coming from different languages.
|
Copyright © 1999-2021 by the D Language Foundation