missing __traits(isArray, T)? what's the difference between __traits & std.traits? can we remove one to avoid dup?

Luís Ferreira contact at lsferreira.net
Sun Jun 6 01:26:33 UTC 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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20210606/a7cde42e/attachment.sig>


More information about the Digitalmars-d mailing list