www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Need for std.meta.isSame over __traits(isSame)

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Can somebody explain the need for

```d
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

```d
__traits(isSame, a, b)
```

?

Are there any cases where

```d
__traits(isSame, a, b)
```

doesn't have the same value as

```d
a == b
```

provided the static if expression above is true.
Sep 01 2021
next sibling parent reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
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`.
Sep 01 2021
parent user1234 <user1234 12.de> writes:
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: ```d 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.
Sep 01 2021
prev sibling parent user1234 <user1234 12.de> writes:
On Wednesday, 1 September 2021 at 22:51:40 UTC, Per Nordlöw wrote:
 Can somebody explain the need for

 ```d
 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

 ```d
 __traits(isSame, a, b)
 ```

 ?

 Are there any cases where

 ```d
 __traits(isSame, a, b)
 ```

 doesn't have the same value as

 ```d
 a == b
 ```

 provided the static if expression above is true.
the traits does not work on literals passed by _AliasTemplateParameter_. ```d 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`
Sep 01 2021