RealyUniqueName / Safety

Null safety for Haxe
MIT License
54 stars 5 forks source link

Another proposal for single-thread environments #15

Closed restorer closed 5 years ago

restorer commented 5 years ago
class NullableStringHolder {
    public var s : Null<String>;

    public function new(s : Null<String>) {
        this.s = s;
    }
}

class NotABugButProposal10 {
    public function proposal() : Void {
        var h = new NullableStringHolder((Math.random() > 0.5) ? "A" : null);

        if (h.s != null) {
            // Safety: Cannot access "length" of a nullable value.
            // While it makes sense in case of multithreading, is it possible to add something like `@:safety(threadsafe)`?
            h.s.length;
        }
    }
}

See https://github.com/restorer/haxe-safety-bugs/blob/master/safetybugs/Main.hx#L140 for working example.

restorer commented 5 years ago

This is for Haxe 4.0.0-preview.5 and Safety from master.

RealyUniqueName commented 5 years ago

It also doesn't make sense for a single-threaded environment if there are some calls before the access to h.s.length. I'm not sure if it worth to change.

restorer commented 5 years ago

Hmm. Something like

if (h.s != null) {
    h.s = null;
    h.s.length;
}

Make sense. Kotlin do the same thing: Smart cast to 'String' is impossible, because 'h.s' is a mutable property that could have been changed by this time.

New proposal: maybe more descriptive error message (like in kotlin)?

RealyUniqueName commented 5 years ago

Duplicated in the official Haxe repo: https://github.com/HaxeFoundation/haxe/issues/7723