Problem with opCast(bool) for classes

bearophile bearophileHUGS at lycos.com
Tue Mar 9 05:08:29 PST 2010


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");
    else
        writeln("fstruct false"); // fstruct false

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

    if (cast(bool)fclass)
        writeln("fclass true");
    else
        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.

Bye,
bearophile



More information about the Digitalmars-d mailing list