HeapsIO / hxbit

Haxe Binary serialization and network synchronization library
155 stars 30 forks source link

Null safety project support #50

Open kyubuns opened 4 years ago

kyubuns commented 4 years ago
class Main {
    static function main() {
        trace((new hxbit.Serializer()).serialize(new Hoge([1,2,3])).toHex());
    }
}

class Hoge implements hxbit.Serializable {
    public function new(fuga: Array<Int>) {
        this.fuga = fuga;
    }

    @:s public var fuga: Array<Int>;
}
-main Main
-lib hxbit
--interp
--macro nullSafety("Main", Strict)
> haxe build.hxml
Main.hx:12: characters 6-34 : Null safety: Cannot assign nullable value here.

I want to use nullSafety in my project. -D hxbit_nullsafety flag, Initialize with the default value, not null.

ncannasse commented 4 years ago

That's not a correct fix. I think instead the correct type should be Null<Array> and then additional fixes in macro to be able to assign nullables to not nullable field (which would then alloc empty array)

kyubuns commented 4 years ago

Sounds good!

As an aside, I discovered this bug when I tried to Serialize Null in the same way. https://github.com/HaxeFoundation/haxe/issues/9544

This is the problem with this line. https://github.com/HeapsIO/hxbit/blob/master/hxbit/Macros.hx#L463