digitalmars.D.learn - alias vs enum for lambdas?
In which programming scenarios should alias be used instead of enum? So far I have only found a simplified notation of a generic lambda: alias id = (x) => x; x; , which does not work in the case of enum. There is also a difference in overloading a non-generic lambda: alias m = (int x) => x; alias m = (float x) => 0.5 + x; - alias overloads a function with the same name. enum e = (int x) => x; ///enum e = (float x) => x; //error - enum does not. At the compiler level, enum lambda is represented as a literal. But how is alias represented? As an expression?
Apr 27
On Monday, 28 April 2025 at 04:59:24 UTC, Orion wrote:In which programming scenarios should alias be used instead of enum? So far I have only found a simplified notation of a generic lambda: alias id = (x) => x; x; , which does not work in the case of enum. There is also a difference in overloading a non-generic lambda: alias m = (int x) => x; alias m = (float x) => 0.5 + x; - alias overloads a function with the same name. enum e = (int x) => x; ///enum e = (float x) => x; //error - enum does not. At the compiler level, enum lambda is represented as a literal. But how is alias represented? As an expression?Alias is the proper way, there's specifications for them to work as a way to overload whereas with enum there's no support. Rememeber that ``` enum e = (int x) => x; ``` is a shortcut to ``` enum e { e = (int x) => x } ``` so always use `alias`.
Apr 28
On Monday, 28 April 2025 at 04:59:24 UTC, Orion wrote:In which programming scenarios should alias be used instead of enum? So far I have only found a simplified notation of a generic lambda: alias id = (x) => x; x; , which does not work in the case of enum. There is also a difference in overloading a non-generic lambda: alias m = (int x) => x; alias m = (float x) => 0.5 + x; - alias overloads a function with the same name. enum e = (int x) => x; ///enum e = (float x) => x; //error - enum does not. At the compiler level, enum lambda is represented as a literal. But how is alias represented? As an expression?aliases for types, overload sets enum for litterals
Apr 28
On Monday, 28 April 2025 at 20:05:34 UTC, monkyyy wrote:aliases for types, overload sets enum for litteralsAccording to me that's a very bad advice. I'm more on "never enum". Do you have any example where enum is better ?
Apr 28
On Monday, 28 April 2025 at 22:16:47 UTC, user1234 wrote:On Monday, 28 April 2025 at 20:05:34 UTC, monkyyy wrote:enum N=1024; int[N] array1; int[N] array2;aliases for types, overload sets enum for litteralsAccording to me that's a very bad advice. I'm more on "never enum". Do you have any example where enum is better ?
Apr 28
On Monday, 28 April 2025 at 22:47:35 UTC, monkyyy wrote:On Monday, 28 April 2025 at 22:16:47 UTC, user1234 wrote:this is totally unrelated to the topic. i.e function as expressions.On Monday, 28 April 2025 at 20:05:34 UTC, monkyyy wrote:enum N=1024; int[N] array1; int[N] array2;aliases for types, overload sets enum for litteralsAccording to me that's a very bad advice. I'm more on "never enum". Do you have any example where enum is better ?
Apr 28