Thread overview
Need for std.meta.isSame over __traits(isSame)
Sep 01, 2021
Per Nordlöw
Sep 01, 2021
Per Nordlöw
Sep 01, 2021
user1234
Sep 01, 2021
user1234
September 01, 2021

Can somebody explain the need for

private template isSame(alias a, alias b)
{
    static if (!is(typeof(&a && &b)) // at least one is an rvalue
            && __traits(compiles, { enum isSame = a == b; })) // c-t comparable
    {
        enum isSame = a == b;
    }
    else
    {
        enum isSame = __traits(isSame, a, b);
    }
}

when there is already

__traits(isSame, a, b)

?

Are there any cases where

__traits(isSame, a, b)

doesn't have the same value as

a == b

provided the static if expression above is true.

September 01, 2021

On Wednesday, 1 September 2021 at 22:51:40 UTC, Per Nordlöw wrote:

>

Can somebody explain the need for

Ok, __traits(isSame) always returns false for values.

This is very unfortunate as std.traits.isSame is one of the most used template instances in typical std.meta-programming and has a noticeable impact on time and space complexity now that AliasAssign-enabled versions of std.meta members have removed the need for other costly recursive template patterns.

I suggest we add a new builtin trait that exactly mimics std.traits.isSame or inline the calls to isSame in std.traits.meta. This is gonna significantly speed up functions in std.meta, for instance staticIndexOf, EraseAll, GenericReplace, ReplaceAll, Pack.

September 01, 2021

On Wednesday, 1 September 2021 at 22:51:40 UTC, Per Nordlöw wrote:

>

Can somebody explain the need for

private template isSame(alias a, alias b)
{
    static if (!is(typeof(&a && &b)) // at least one is an rvalue
            && __traits(compiles, { enum isSame = a == b; })) // c-t comparable
    {
        enum isSame = a == b;
    }
    else
    {
        enum isSame = __traits(isSame, a, b);
    }
}

when there is already

__traits(isSame, a, b)

?

Are there any cases where

__traits(isSame, a, b)

doesn't have the same value as

a == b

provided the static if expression above is true.

the traits does not work on literals passed by AliasTemplateParameter.

enum isSame1(alias a, alias b) = a == b;
enum isSame2(alias a, alias b) = __traits(isSame, a, b);

pragma(msg, isSame1!(0, 0));   // true
pragma(msg, isSame2!(0, 0));   // false

This looks a bit like a bug, because __traits(isSame, 0, 0) yields true

September 01, 2021

On Wednesday, 1 September 2021 at 23:04:18 UTC, Per Nordlöw wrote:

>

On Wednesday, 1 September 2021 at 22:51:40 UTC, Per Nordlöw wrote:

>

Can somebody explain the need for

Ok, __traits(isSame) always returns false for values.

This is very unfortunate as std.traits.isSame is one of the most used template instances in typical std.meta-programming and has a noticeable impact on time and space complexity now that AliasAssign-enabled versions of std.meta members have removed the need for other costly recursive template patterns.

I suggest we add a new builtin trait that exactly mimics std.traits.isSame or inline the calls to isSame in std.traits.meta. This is gonna significantly speed up functions in std.meta, for instance staticIndexOf, EraseAll, GenericReplace, ReplaceAll, Pack.

I suggest to change the template signature instead:

template isSame(Args...)
if (Args.length == 2)
{
    enum isSame = __traits(isSame, Args[0], Args[1]);
}

The problem is not __traits(isSame), it's more the TemplateAliasParameter, as observed previously.