Thread overview
Inplace lambda execution
4 days ago
confuzzled
4 days ago
monkyyy
4 days ago
Salih Dincer
4 days ago

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:

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

4 days ago

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:

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

4 days ago

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.

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