www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - alias vs enum for lambdas?

reply Orion <vlad000ff yopmail.com> writes:
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
next sibling parent user1234 <user1234 12.de> writes:
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
prev sibling next sibling parent reply monkyyy <crazymonkyyy gmail.com> writes:
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
parent reply user1234 <user1234 12.de> writes:
On Monday, 28 April 2025 at 20:05:34 UTC, monkyyy wrote:
 aliases for types, overload sets

 enum for litterals
According 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
parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Monday, 28 April 2025 at 22:16:47 UTC, user1234 wrote:
 On Monday, 28 April 2025 at 20:05:34 UTC, monkyyy wrote:
 aliases for types, overload sets

 enum for litterals
According to me that's a very bad advice. I'm more on "never enum". Do you have any example where enum is better ?
enum N=1024; int[N] array1; int[N] array2;
Apr 28
parent user1234 <user1234 12.de> writes:
On Monday, 28 April 2025 at 22:47:35 UTC, monkyyy wrote:
 On Monday, 28 April 2025 at 22:16:47 UTC, user1234 wrote:
 On Monday, 28 April 2025 at 20:05:34 UTC, monkyyy wrote:
 aliases for types, overload sets

 enum for litterals
According to me that's a very bad advice. I'm more on "never enum". Do you have any example where enum is better ?
enum N=1024; int[N] array1; int[N] array2;
this is totally unrelated to the topic. i.e function as expressions.
Apr 28
prev sibling next sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Sunday, April 27, 2025 10:59:24 PM MDT Orion via Digitalmars-d-learn 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 create an alternate name for a symbol (including types), whereas enums are values which have no address (rather their value is essentially copy-pasted when they're used). So, enums are essentially variables except that they have no storage associated with them, whereas aliases just give names to things. And the name of an alias is replaced with the original name whenever the alias is used. So, if you alias a function, it makes sense that you can have overloads. It's just the same function with a different name. Lambdas are a bit weird in that respect in that they're anonymous, but the alias is giving them names, and as such overloading makes sense. However, an enum must be a value, and a function is not a value. A function pointer could be, but a function cannot be - and neither can a lambda, because a lambda is an anonymous function, not a function pointer. Basically, if it doesn't make sense for something to be assigned to a variable, then it doesn't make sense for it to be an enum. - Jonathan M Davis
May 01
prev sibling next sibling parent Paul Backus <snarwin gmail.com> writes:
On Monday, 28 April 2025 at 04:59:24 UTC, Orion wrote:
 At the compiler level, enum lambda is represented as a literal.
 But how is alias represented? As an expression?
A lambda is basically syntax sugar for a local function declaration. So when you write this: fun((float x) => 0.5 + x); ...the compiler replaces it internally with code that looks like this: auto __lambda(float x) => 0.5 + x; fun(&__lambda); When the lambda is generic, the compiler generates a template function instead of a regular function: // Before: range.map!(x => 0.5 + x); // After: auto __lambda(T)(T x) => 0.5 + x; range.map!(__lambda); So, when you use `alias` to give a name to a lambda, what you are really doing is giving a name to this internal function or template that the compiler replaces the lambda with. Of course, at that point, you might as well just write a named function directly: // Instead of this: alias m = (float x) => 0.5 + x; // You can just write this: auto m(float x) => 0.5 + x;
May 01
prev sibling parent reply Orion <vlad000ff yopmail.com> writes:
Yes. But at the same time, defining a function via alias is a 1st 
class function, unlike a standard function definition!

alias af = (int x) => x;
auto sf(int x) => x;

auto v = af;
auto p = &sf;

This means that the definitions are not equivalent.

In essence, an alias function behaves like a lambda function 
assigned to a variable, but at the same time there is the 
possibility of adhoc overloading, which is only possible for a 
regular/standard function.
May 02
parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Friday, 2 May 2025 at 13:55:46 UTC, Orion wrote:
 Yes. But at the same time, defining a function via alias is a 
 1st class function, unlike a standard function definition!

 alias af = (int x) => x;
 auto sf(int x) => x;

 auto v = af;
 auto p = &sf;

 This means that the definitions are not equivalent.

 In essence, an alias function behaves like a lambda function 
 assigned to a variable, but at the same time there is the 
 possibility of adhoc overloading, which is only possible for a 
 regular/standard function.
This is still a continuation of aliases are for types, enums for literals ```d alias F=(i)=>i+1; import std; void main(){ F(3).writeln; F(3.14).writeln; } ``` F cant be enum here, because the type inference comes latter taking a pointer needs to be literal
May 02
parent Nick Treleaven <nick geany.org> writes:
On Friday, 2 May 2025 at 16:53:05 UTC, monkyyy wrote:
 This is still a continuation of aliases are for types, enums 
 for literals

 ```d
 alias F=(i)=>i+1;
```d pragma(msg, is(F)); // false, F is not a type ``` Even if we give `i` a type: ```d alias F=(int i)=>i+1; pragma(msg, is(F)); // still false ``` And F is clearly bound to a literal - a function literal.
May 03