www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19203] New: alias this to a bool behaves both inconsistently

https://issues.dlang.org/show_bug.cgi?id=19203

          Issue ID: 19203
           Summary: alias this to a bool behaves both inconsistently and
                    incorrectly with static assert
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: iamthewilsonator hotmail.com

struct BoolWithErr {
    bool b;
    string error;
    alias b this;
}

struct Foo {}

template hasPopBack(T) {
    static if (!is(typeof(T.init.popBack)))
        enum hasPopBack = BoolWithErr(false, T.stringof~" does not have
popBack");
    else
        enum hasPopBack = BoolWithErr(true,"");
}

int main()
{   
    // Fine:
    auto a = isArrayLike!Foo && isArrayLike!Foo; // false
    enum b = isArrayLike!Foo && isArrayLike!Foo; // false
    pragma(msg, typeof(a)); // bool

    // Fine:
    // Error: static assert: b is false
    static assert(b);

    // Dubious: alias this b should work
    // Error: expression isArrayLike!(Foo) of type BoolWithErr does not have a
boolean value
    //   while evaluating: static assert(isArrayLike!(Foo))
    static assert(isArrayLike!Foo);

    // Bad: typeof the expression is bool
    // Error: expression isArrayLike!(Foo) && isArrayLike!Foo of type
BoolWithErr does not have a boolean value
    //    while evaluating: static assert(isArrayLike!(Foo) && isArrayLike!Foo)
    static assert(isArrayLike!Foo && isArrayLike!Foo);

    return 0;
}

--
Aug 28 2018