RealyUniqueName / Safety

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

Safety: Cannot unify safetybugs.TypedHolder<Null<String>> with safetybugs.TypedHolder<String> #14

Closed restorer closed 5 years ago

restorer commented 5 years ago
class TypedHolder<T> {
    public var v : T;

    public function new(v : T) {
        this.v = v;
    }
}

class Bug9 {
    public function bug() : Void {
        var s : Null<String> = ((Math.random() > 0.5) ? "A" : null);

        if (s != null) {
            // Safety: Cannot unify safetybugs.TypedHolder<Null<String>> with safetybugs.TypedHolder<String>
            var h : TypedHolder<String> = new TypedHolder(s);
        }
    }
}

See https://github.com/restorer/haxe-safety-bugs/blob/master/safetybugs/Main.hx#L128 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

Unfortunately, this is not avoidable in the plugin, because Safety cannot modify the typing process. Use new TypeHolder((s:String)) as a workaround.

restorer commented 5 years ago

Currently I use .unsafe(), it looks better for me. Probably this should be mentioned in documentation somewhere.

RealyUniqueName commented 5 years ago

.unsafe() is not safe :) If you ever remove or change if(s != null), your code will become unsafe. While with (s:String) it will emit a compilation error.

RealyUniqueName commented 5 years ago

Another option is new TypeHolder<String>(s).