Can we just have struct inheritence already?
XavierAP
n3minis-git at yahoo.es
Sat Jun 15 14:08:42 UTC 2019
On Saturday, 15 June 2019 at 13:39:13 UTC, Timon Gehr wrote:
>
> It does not break the spec:
>
> "Undefined Behavior: If a void initialized variable's value is
> used before it is set, the behavior is undefined."
>
> https://dlang.org/spec/declaration.html#void_init
You're right about the spec... Still void initialization of bool
should be illegal, in @safe code (as void init pointers and refs
already is) or better yet in any case.
If you're going the squeeze route of void initializations, you'd
better use ints, no point in a bool abstraction.
>> - I never understood why D adopted over from C/C++ that bool
>> is int and implicitly convertible; I don't understand when
>> this may be useful.
>
> It can be useful, but it is not necessarily worth the savings
> (because (b?1:0) is so short).
Plus you can write these as extension methods, as I used to do in
C# (which makes a design point of allowing no casting, implicit
nor explicit, either way between bool and int):
bool tobool(T)(T i) { return i != 0; }
int toint(bool b) { return b?1:0; }
> Another case where it may be useful is if you like to
> initialize your bools using literals `0` and `1`.
Implicit conversion from bool to int is much more dangerous than
int to bool (plus people may really want if(int) for interop).
And two-ways implicit cast is also inconsistent with
static assert( is(bool:int) );
static assert(!is(int:bool) );
The pitfalls of implicitness... Explicit implies readable.
More information about the Digitalmars-d
mailing list