www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Inplace lambda execution

reply confuzzled <con fuzzled.com> writes:
I apologize for the newbie question but how do I get the lambda 
the following to execute and return a string instead of the 
function pointer?:

private static immutable string[] typeNames = [staticMap!(T => 
typeof(T).stringof, TypeSeq)];

I currently get this error:

```D
sumtype.d(61): Error: cannot implicitly convert expression `(int 
T) => "int"` of type `string function(int T) pure nothrow  nogc 
 safe` to `immutable(string)`
     private static immutable string[] typeNames = [staticMap!(T 
=> typeof(T).stringof, TypeSeq)];
                                                    ^
```

Thanks in advance.

-- confuzzled
Mar 28
next sibling parent monkyyy <crazymonkyyy gmail.com> writes:
On Friday, 28 March 2025 at 12:12:45 UTC, confuzzled wrote:
 I apologize for the newbie question but how do I get the lambda 
 the following to execute and return a string instead of the 
 function pointer?:

 private static immutable string[] typeNames = [staticMap!(T => 
 typeof(T).stringof, TypeSeq)];

 I currently get this error:

 ```D
 sumtype.d(61): Error: cannot implicitly convert expression 
 `(int T) => "int"` of type `string function(int T) pure nothrow 
  nogc  safe` to `immutable(string)`
     private static immutable string[] typeNames = [staticMap!(T 
 => typeof(T).stringof, TypeSeq)];
                                                    ^
 ```

 Thanks in advance.

 -- confuzzled
static map cant take a lamda; the syntax of templates are all uglier `enum F(T)=T.stringof; enum typeNames = [staticMap!(F, TypeSeq)];` or something
Mar 28
prev sibling parent Salih Dincer <salihdb hotmail.com> writes:
On Friday, 28 March 2025 at 12:12:45 UTC, confuzzled wrote:
 I currently get this error:

 sumtype.d(61): Error: cannot implicitly convert expression 
 `(int T) => "int"` of type `string function(int T) pure nothrow 
  nogc  safe` to `immutable(string)`
     private static immutable string[] typeNames = [staticMap!(T 
 => typeof(T).stringof, TypeSeq)];
In your original code, `typeof(T).stringof` was potentially unnecessary, as `T` itself already refers to the type. The `.stringof` property will convert the type directly to its string representation. ```d alias strADLANIM = strRepresent; template strRepresent(T) { enum strRepresent = T.stringof; } void main() { import std.meta : AliasSeq, staticMap; alias seq = AliasSeq!(int, string, double); auto typeNames = [staticMap!(strRepresent, seq)]; import std.stdio : prn = writefln; typeNames.prn!"%(%s\n%)"; // Will output something like: // "int" // "string" // "double" } ``` This will give you an array of type names as strings. SDB 79
Mar 28