www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - missing __traits(isArray, T)? what's the difference between __traits &

reply mw <mingwu gmail.com> writes:
Hi,

I'm checking:

https://dlang.org/library/object/destroy.html
```
...
if (__traits(isStaticArray, T));
```
and want to try `__traits(isArray, T)` in my code, but the 
compiler complains it's not there,

Then I found std.traits.isArray!T here:

https://dlang.org/phobos/std_traits.html

which has much more traits defined than:

https://dlang.org/spec/traits.html#isStaticArray ...

I read both can be used at compile time, I'm wondering is there 
any difference between __traits & std.traits?

If there is no difference between this two mechanisms, should we 
completely move all the `__traits` stuff into std.traits to avoid 
duplication?
Jun 05
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 6 June 2021 at 00:52:32 UTC, mw wrote:
 and want to try `__traits(isArray, T)` in my code, but the 
 compiler complains it's not there,
You can check is array with an is expression `is(T == E[], E)`.
 I read both can be used at compile time, I'm wondering is there 
 any difference between __traits & std.traits?
The original idea was that __traits would be the raw language built-in, then std.traits is the refined library interface. std.traits uses some __traits, some is(), and some other miscellaneous techniques together under the hood. tbh my personal view is to ditch std.traits and just teach people the pure language techniques.
Jun 05
prev sibling next sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Sunday, 6 June 2021 at 00:52:32 UTC, mw wrote:
 I read both can be used at compile time, I'm wondering is there 
 any difference between __traits & std.traits?

 If there is no difference between this two mechanisms, should 
 we completely move all the `__traits` stuff into std.traits to 
 avoid duplication?
`__traits` expressions are compiler builtins. The templates in `std.traits` are library code. They might be implemented using `__traits`, or `is(...)` expressions, or some other kind of introspection. In general, it's not possible to move `__traits` expressions into library code, because, as compiler builtins, they're able to do things that can't be done any other way.
Jun 05
parent reply mw <mingwu gmail.com> writes:
On Sunday, 6 June 2021 at 01:06:26 UTC, Paul Backus wrote:
 On Sunday, 6 June 2021 at 00:52:32 UTC, mw wrote:
 I read both can be used at compile time, I'm wondering is 
 there any difference between __traits & std.traits?

 If there is no difference between this two mechanisms, should 
 we completely move all the `__traits` stuff into std.traits to 
 avoid duplication?
`__traits` expressions are compiler builtins. The templates in `std.traits` are library code. They might be implemented using `__traits`, or `is(...)` expressions, or some other kind of introspection. In general, it's not possible to move `__traits` expressions into library code, because, as compiler builtins, they're able to do things that can't be done any other way.
Thanks for the explanation, then can we at least remove the duplicates from the library? e.g. https://dlang.org/phobos/std_traits.html#isStaticArray https://dlang.org/spec/traits.html#isStaticArray
Jun 05
parent Paul Backus <snarwin gmail.com> writes:
On Sunday, 6 June 2021 at 01:17:23 UTC, mw wrote:
 Thanks for the explanation, then can we at least remove the 
 duplicates from the library? e.g.

 https://dlang.org/phobos/std_traits.html#isStaticArray

 https://dlang.org/spec/traits.html#isStaticArray
Removing public symbols from the standard library would break backwards compatibility. Better to just live with the duplication.
Jun 05
prev sibling parent =?ISO-8859-1?Q?Lu=EDs?= Ferreira <contact lsferreira.net> writes:
On Sun, 2021-06-06 at 00:52 +0000, mw via Digitalmars-d wrote:
 Hi,
=20
 I'm checking:
=20
 https://dlang.org/library/object/destroy.html
 ```
 ...
 if (__traits(isStaticArray, T));
 ```
 and want to try `__traits(isArray, T)` in my code, but the=20
 compiler complains it's not there,
=20
 Then I found std.traits.isArray!T here:
=20
 https://dlang.org/phobos/std_traits.html
=20
 which has much more traits defined than:
=20
 https://dlang.org/spec/traits.html#isStaticArray=C2=A0...
=20
 I read both can be used at compile time, I'm wondering is there=20
 any difference between __traits & std.traits?
=20
 If there is no difference between this two mechanisms, should we=20
 completely move all the `__traits` stuff into std.traits to avoid=20
 duplication?
=20
I guess you are confused. You shouldn't mix std.traits definitions with `__traits` expression. `isArray` is defined by the standard library and `__traits` is a language thing. That's why `__traits(isArray, T)` doesn't work. Some fundamental checks are implemented by the compiler using `__traits`. `__traits` is an expression to get information from the compiler, similarly to `pragma`, but for some kind of compile-time reflection and get other useful compiler information from types and symbols. On the other hand, some other complex checks are in the standard library and uses a combination of `__traits`, `is(...)`, etc. `std.traits` is a module from the standard library that provides an extension to types and symbols information extraction, built on top of `__traits` and other things like `is(...)` and other weird stuff. The idea of this module is to simplify this tedious process checking and fetching certain information which requires multiple steps. Some traits are actually "duplicated" but are very useful for certain operations like, when you are using a staticMap from `std.meta`. Defining `enum bool isStaticArray(T) =3D __traits(isStaticArray, T);` is to be able to do this: `allSatisfy!(isStaticArray, ...)` --=20 Sincerely, Lu=C3=ADs Ferreira lsferreira.net
Jun 05