Re: Value or Reference Semantics Trait

2014-01-13 Thread qznc

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.


Re: Value or Reference Semantics Trait

2014-01-13 Thread John Colvin

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.


Re: Value or Reference Semantics Trait

2014-01-13 Thread Tobias Pankrath

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


Re: Value or Reference Semantics Trait

2014-01-13 Thread Orvid King
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 tob...@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




Re: Value or Reference Semantics Trait

2014-01-13 Thread Dicebot

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)


Re: Value or Reference Semantics Trait

2014-01-13 Thread anonymous

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)


Re: Value or Reference Semantics Trait

2014-01-13 Thread Orvid King
On 1/13/14, anonymous anonym...@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.


Value or Reference Semantics Trait

2014-01-12 Thread Nordlöw
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.