Thread overview
missing __traits(isArray, T)? what's the difference between __traits & std.traits? can we remove one to avoid dup?
Jun 06, 2021
mw
Jun 06, 2021
Adam D. Ruppe
Jun 06, 2021
Paul Backus
Jun 06, 2021
mw
Jun 06, 2021
Paul Backus
Jun 06, 2021
Luís Ferreira
June 06, 2021

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?

June 06, 2021

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.

June 06, 2021

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.

June 06, 2021

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

June 06, 2021
On Sun, 2021-06-06 at 00:52 +0000, mw via Digitalmars-d wrote:
> 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?
> 

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) = __traits(isStaticArray, T);` is
to be able to do this: `allSatisfy!(isStaticArray, ...)`

-- 
Sincerely,
Luís Ferreira @ lsferreira.net



June 06, 2021

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.