xxxxxxxxxx
final class Bool {
immutable bool value;
alias value this;
this(bool b) nothrow pure { this.value = b; }
}
struct ConstBool {
private immutable Bool _b;
Bool _uncast() const nothrow pure {
// It is safe to convert an immutable Bool to a mutable Bool
// because even a mutable Bool has no mutable fields (and
// cannot have any because Bool is a final class).
return cast() this._b;
}
alias _uncast this;
this(const Bool b) nothrow pure {
// Can convert mutable bool to immutable by same logic as above.
this._b = cast(immutable) b;
}
// https://forum.dlang.org/post/eicpsjarvxvahknluqwu@forum.dlang.org
T opCast(T)() const {
static if (is(bool : T))
return _b.value;
else
return cast(T) this._uncast;
}
}
immutable TRUE = ConstBool(new Bool(true)), FALSE = ConstBool(new Bool(false));
static assert(!FALSE); // Works in DMD 2.067.1 through 2.087.1 but in DMD 2.088.0-beta.1-master-ede5969 fails with "Error: expression Bool(false) is not constant"
static assert(!!TRUE); // Works in DMD 2.067.1 through 2.087.1 but in DMD 2.088.0-beta.1-master-ede5969 fails with "Error: expression Bool(true) is not constant"
void main()
{
}