Thread overview | |||||
---|---|---|---|---|---|
|
December 27, 2020 How to reliably detect an alias sequence? | ||||
---|---|---|---|---|
| ||||
Given a name bound to an alias sequence, what is a reliable way to detect that? import std.meta: AliasSeq; alias a = AliasSeq!int; static if (<is a an AliasSeq>) { } __traits(isSame, a, AliasSeq!a) could work but doesn't, because it flattens singletons, which means __traits(isSame, int, AliasSeq!int) is true. (A bug/bad design, IMO. If the intended behavior is not to flatten tuples passed to __traits, there is no reason to make a special case for singletons). |
December 27, 2020 Re: How to reliably detect an alias sequence? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | On Sunday, 27 December 2020 at 12:23:26 UTC, Max Samukha wrote:
> Given a name bound to an alias sequence, what is a reliable way to detect that?
>
> import std.meta: AliasSeq;
>
> alias a = AliasSeq!int;
>
> static if (<is a an AliasSeq>) {
>
> }
>
> __traits(isSame, a, AliasSeq!a) could work but doesn't, because it flattens singletons, which means __traits(isSame, int, AliasSeq!int) is true. (A bug/bad design, IMO. If the intended behavior is not to flatten tuples passed to __traits, there is no reason to make a special case for singletons).
This should work:
```
static assert(is(int == AliasSeq!int));
Drepl: static assert: `is(int == (int))` is false
```
The most appropriate solution would be:
```
AliasSeq!int.stringof == (int);
```
But it wouldn't exclude false positives.
|
December 28, 2020 Re: How to reliably detect an alias sequence? | ||||
---|---|---|---|---|
| ||||
Posted in reply to sighoya | On Sunday, 27 December 2020 at 18:25:10 UTC, sighoya wrote: > > This should work: > ``` > static assert(is(int == AliasSeq!int)); > Drepl: static assert: `is(int == (int))` is false > ``` > That will only work for type tuples. We need a general solution that would work for any alias tuple. > The most appropriate solution would be: > ``` > AliasSeq!int.stringof == (int); > ``` > > But it wouldn't exclude false positives Yes, stringof is inadequate. |
Copyright © 1999-2021 by the D Language Foundation