www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Alias Vs. Enum?

reply Rubn <where is.this> writes:
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?
Jan 06 2018
next sibling parent reply Rubn <where is.this> writes:
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?
Jan 06 2018
parent Timon Gehr <timon.gehr gmx.ch> writes:
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.)
Jan 06 2018
prev sibling parent reply Stefan Koch <uplink.coder googlemail.com> writes:
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 :)
Jan 06 2018
parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
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
Jan 07 2018
parent sarn <sarn theartofmachinery.com> writes:
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.
Jan 07 2018