digitalmars.D.learn - Value or Reference Semantics Trait
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (5/5) Jan 12 2014 Is there a trait to check whether a type has value or reference
- qznc (9/14) Jan 13 2014 No, afaik.
- John Colvin (15/20) Jan 13 2014 That's a complicated question in D. Only simple cases fit one or
- Tobias Pankrath (2/7) Jan 13 2014 Adding to the other answers: std.traits.hasIndirections
- Orvid King (5/13) Jan 13 2014 If you just want to check if specifically it's a structure, you could
- Dicebot (2/9) Jan 13 2014 is(T == struct)
- anonymous (2/9) Jan 13 2014 is(T == struct)
- Orvid King (9/18) Jan 13 2014 Or that, yeah XD (For some reason I was thinking about ensuring that
Is there a trait to check whether a type has value or reference semantics? I need this in a template struct that adaptively (using static if) represent histogram bins as either a dense static array or a sparse associative array.
Jan 12 2014
On Sunday, 12 January 2014 at 20:16:15 UTC, Nordlöw wrote:Is there a trait to check whether a type has value or reference semantics? I need this in a template struct that adaptively (using static if) represent histogram bins as either a dense static array or a sparse associative array.No, afaik. However, the question goes deeper than you might have though. You probably want to distuingish between structs+scalars vs classes. However, what about ref-arguments? Mutable arrays are struct values, but have reference semantics. Immutable-value arrays (e.g. string) have value semantics. You could use one version as default and define special cases via some stuff you find in std.traits.
Jan 13 2014
On Sunday, 12 January 2014 at 20:16:15 UTC, Nordlöw wrote:Is there a trait to check whether a type has value or reference semantics? I need this in a template struct that adaptively (using static if) represent histogram bins as either a dense static array or a sparse associative array.That's a complicated question in D. Only simple cases fit one or the other exactly. E.g. A class always has reference semantics, but may contain any combination of reference and value semantic fields. A struct has value semantics, but may contain any combination of reference and value semantic fields. A slice has value semantics w.r.t. its length and start point, but reference semantics for its data. A static array has value semantics. For your problem you should look at the set of IsSomething traits in the table at the top of http://dlang.org/phobos-prerelease/std_traits.html as they will hopefully be sufficient.
Jan 13 2014
On Sunday, 12 January 2014 at 20:16:15 UTC, Nordlöw wrote:Is there a trait to check whether a type has value or reference semantics? I need this in a template struct that adaptively (using static if) represent histogram bins as either a dense static array or a sparse associative array.Adding to the other answers: std.traits.hasIndirections
Jan 13 2014
If you just want to check if specifically it's a structure, you could always check `__traits(compiles, T()) && if(typeof(T()) == T)` beware however that this will evaluate to true if T is a class with a static opCall who's return type is T. On 1/13/14, Tobias Pankrath <tobias pankrath.net> wrote:On Sunday, 12 January 2014 at 20:16:15 UTC, Nordlöw wrote:Is there a trait to check whether a type has value or reference semantics? I need this in a template struct that adaptively (using static if) represent histogram bins as either a dense static array or a sparse associative array.Adding to the other answers: std.traits.hasIndirections
Jan 13 2014
On Monday, 13 January 2014 at 13:41:30 UTC, Orvid King wrote:If you just want to check if specifically it's a structure, you could always check `__traits(compiles, T()) && if(typeof(T()) == T)` beware however that this will evaluate to true if T is a class with a static opCall who's return type is T.is(T == struct)
Jan 13 2014
On Monday, 13 January 2014 at 13:41:30 UTC, Orvid King wrote:If you just want to check if specifically it's a structure, you could always check `__traits(compiles, T()) && if(typeof(T()) == T)` beware however that this will evaluate to true if T is a class with a static opCall who's return type is T.is(T == struct)
Jan 13 2014
On 1/13/14, anonymous <anonymous example.com> wrote:On Monday, 13 January 2014 at 13:41:30 UTC, Orvid King wrote:Or that, yeah XD (For some reason I was thinking about ensuring that it was constructible, which, after reading the question again, isn't actually needed, woops :D) With that in mind, value type semantics would be `enum hasValueSemantics(T) = is(T == struct) || is(T == enum) || is(T == union) || isArray!T;` Be aware however that this assumes you want arrays to be considered to have value semantics due to their length and base element being by-value.If you just want to check if specifically it's a structure, you could always check `__traits(compiles, T()) && if(typeof(T()) == T)` beware however that this will evaluate to true if T is a class with a static opCall who's return type is T.is(T == struct)
Jan 13 2014