www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Value or Reference Semantics Trait

reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
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
next sibling parent "qznc" <qznc web.de> writes:
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
prev sibling next sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
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
prev sibling parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
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
parent reply Orvid King <blah38621 gmail.com> writes:
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
next sibling parent "Dicebot" <public dicebot.lv> writes:
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
prev sibling parent reply "anonymous" <anonymous example.com> writes:
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
parent Orvid King <blah38621 gmail.com> writes:
On 1/13/14, anonymous <anonymous example.com> wrote:
 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)
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.
Jan 13 2014