Thread overview
enum template shorthand and short circuit evaluation
Jun 10, 2014
Byron
Jun 10, 2014
Artur Skawina
Jun 10, 2014
Byron Heads
June 10, 2014
Should this work?  It seems like the short circuit booleans are not working:

--------------------------------
import std.traits;

enum isPrimitive(T) = isBasicType!T || (isArray!T && isBasicType!
(ForeachType!T));

void main() {

        assert(isPrimitive!int);
        assert(isPrimitive!char);
        assert(isPrimitive!string);
        assert(isPrimitive!(byte[]));

        assert(!isPrimitive!test1);
        assert(!isPrimitive!test2);
        assert(!isPrimitive!test3);

}

class test1 {}
struct test2 {}
interface test3 {}
----------------------------------------

dmd test
C:\D\dmd2\windows\bin\..\..\src\phobos\std\traits.d(5762): Error: invalid
foreach aggregate 0
test.d(5): Error: template instance std.traits.ForeachType!int error
instantiating
test.d(9):        instantiated from here: isPrimitive!int
test.d(9): Error: template instance test.isPrimitive!int error
instantiating
C:\D\dmd2\windows\bin\..\..\src\phobos\std\traits.d(5762): Error: invalid
foreach aggregate '\xff'
test.d(5): Error: template instance std.traits.ForeachType!char error
instantiating
test.d(10):        instantiated from here: isPrimitive!char
test.d(10): Error: template instance test.isPrimitive!char error
instantiating
C:\D\dmd2\windows\bin\..\..\src\phobos\std\traits.d(5762): Error: invalid
foreach aggregate null
test.d(5): Error: template instance std.traits.ForeachType!(test1) error
instantiating
test.d(14):        instantiated from here: isPrimitive!(test1)
test.d(14): Error: template instance test.isPrimitive!(test1) error
instantiating
C:\D\dmd2\windows\bin\..\..\src\phobos\std\traits.d(5762): Error: invalid
foreach aggregate test2()
test.d(5): Error: template instance std.traits.ForeachType!(test2) error
instantiating
test.d(15):        instantiated from here: isPrimitive!(test2)
test.d(15): Error: template instance test.isPrimitive!(test2) error
instantiating
C:\D\dmd2\windows\bin\..\..\src\phobos\std\traits.d(5762): Error: invalid
foreach aggregate null
test.d(5): Error: template instance std.traits.ForeachType!(test3) error
instantiating
test.d(16):        instantiated from here: isPrimitive!(test3)
test.d(16): Error: template instance test.isPrimitive!(test3) error
instantiating
--------------------------------

DMD32 D Compiler v2.065


But this style works:

template isPrimitive(T)
{
    static if(isBasicType!T || (isArray!T && isBasicType!(ForeachType!T)))
{
        enum isPrimitive = true;
    } else {
        enum isPrimitive = false;
    }
}



-Byron
June 10, 2014
On 06/10/14 02:28, Byron via Digitalmars-d-learn wrote:
> Should this work?  It seems like the short circuit booleans are not working:
> 
> enum isPrimitive(T) = isBasicType!T || (isArray!T && isBasicType!
> (ForeachType!T));

[...]

> But this style works:
> 
> template isPrimitive(T)
> {
>     static if(isBasicType!T || (isArray!T && isBasicType!(ForeachType!T)))
> {
>         enum isPrimitive = true;
>     } else {
>         enum isPrimitive = false;
>     }
> }

Static-if is special. Outside of static-if [1], code needs to be valid, even if it ends up never being executed.

> Should this work?

No. Allowing invalid code just because it will be skipped when the code executes (either at RT or CT) would create other problems, while only solving a minor and relatively rare one (the verbosity of static-if).

artur

[1] and a few other contexts, like in template constraints, is-expressions etc.
June 10, 2014
On Tue, 10 Jun 2014 17:06:08 +0200, Artur Skawina via Digitalmars-d-learn wrote:

> On 06/10/14 02:28, Byron via Digitalmars-d-learn wrote:
>> Should this work?  It seems like the short circuit booleans are not working:
>> 
>> enum isPrimitive(T) = isBasicType!T || (isArray!T && isBasicType!
>> (ForeachType!T));
> 
> 
> No. Allowing invalid code just because it will be skipped when the code executes (either at RT or CT) would create other problems, while only solving a minor and relatively rare one (the verbosity of static-if).
> 
> artur
> 
> [1] and a few other contexts, like in template constraints, is-expressions etc.


Still feels like a bug to me (ie turtles all the way down is not being applied here)