www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Problem with opCast(bool) for classes

reply bearophile <bearophileHUGS lycos.com> writes:
This extended post comes from a note I've written in the announce group that
was ignored.

A opCast(bool) operator present in struct is called automatically in a if(x),
but the same is not true if x is a class. So for example if you modify your
code changing a struct into a class, other parts of the program will silently
stop working (it usually return true if the class reference is not null). This
is a problem.

This shows the situation:

import std.stdio: writeln;

struct FooStruct {
    int x;
    T opCast(T:bool)() {
        return this.x != 0;

class FooClass {
    int x;
    this(int xx) { this.x = xx; }
    static FooClass opCall(int xx) { return new FooClass(xx); }
    T opCast(T:bool)() {
        return this.x != 0;

void main() {
    enum int N = 0;

    auto fstruct = FooStruct(N);
    if (fstruct)
        writeln("fstruct true");
        writeln("fstruct false"); // fstruct false

    auto fclass = FooClass(N);
    if (fclass)
        writeln("fclass true"); // fclass true
        writeln("fclass false");

    if (cast(bool)fclass)
        writeln("fclass true");
        writeln("fclass false"); // fclass false

A possible simple solution is to selectively disallow opCast(bool). So that
conversion of struct to class raises a compile time error that helps avoid the
bugs. But this makes it impossible to have cast(bool) on classes.

Another possible solution is to not remove opCast(bool) from classes and have
if(fclass) call opCast(bool) for classes too, avoiding the asymmetry between
structs and classes. But this requires to write if(fclass is null) to test for
the reference status. This can be an acceptable solution.

Mar 09 2010
parent bearophile <bearophileHUGS lycos.com> writes:
 A possible simple solution is to selectively disallow opCast(bool).

Sorry, I meant: A possible simple solution is to selectively disallow opCast(bool) for classes.
Mar 09 2010