Cysharp / MemoryPack

Zero encoding extreme performance binary serializer for C# and Unity.
MIT License
3.01k stars 182 forks source link

Unions generate code that is not C#7 compliant #304

Open jsupnik-ext-sd opened 1 month ago

jsupnik-ext-sd commented 1 month ago

I have a library that uses C#8 and targets the .net 2.1 standard framework. One of the serialized types is abstract, so I've implemented it as a union in MemoryPack.

MemoryPackGenerator.Emitter.cs generates the following code:

static readonly System.Collections.Generic.Dictionary<Type, ushort> __typeToTag = new(28)

This code causes the compiler to emit the error:

Feature 'target-typed object creation' is not available. Please use language version 9.0 or greater.

A google search of this error message suggests that the issue is the new key word without a corresponding object type after the new.

I believe this problem can be fixed in the source generator by changing line 1092 from:

    static readonly System.Collections.Generic.Dictionary<Type, ushort> __typeToTag = new({{UnionTags.Length}})

to

    static readonly System.Collections.Generic.Dictionary<Type, ushort> __typeToTag = new System.Collections.Generic.Dictionary<Type, ushort>({{UnionTags.Length}})

I'm going to look into creating a PR with a fix for this, but I've never contributed to an OSS project before, so it may take me some time to figure out how to do so.

jsupnik-ext-sd commented 1 month ago

I've created a PR to address this issue: https://github.com/Cysharp/MemoryPack/pull/305 but don't know how to add reviewers.