Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C#] Support System.Collections.Immutable collections as custom containers #1159

Closed
enioluwas opened this issue Jul 14, 2022 · 0 comments · Fixed by #1161
Closed

[C#] Support System.Collections.Immutable collections as custom containers #1159

enioluwas opened this issue Jul 14, 2022 · 0 comments · Fixed by #1161

Comments

@enioluwas
Copy link
Contributor

C# immutable collections are useful data types for building truly immutable Bond structs.

Bond doesn't properly support using them as custom type aliases, for the following reasons:

  1. Generated classes always use the new ImmutableX() as the default constructor, but those do not exist for immutable collections; they are instead constructed with the static field ImmutableX<T>.Empty.
  2. Serialization works, but deserialization fails since the compiler assumes that all BT_LIST/BT_SET aliases properly implement ICollection<T>.Add(), but all mutation methods from ICollection<T> throw a NotSupportedException for immutable collections. Same issue with BT_MAP, but with the setter IDictionary<K, V>.Item[].

The desired mappings would be:

BT_LIST -> ImmutableArray<T>, ImmutableList<T>

BT_SET -> ImmutableHashSet<T>, ImmutableSortedSet<T>

BT_MAP -> ImmutableDictionary<T>, ImmutableSortedDictionary<T>

Based on my initial investigation, the following would need to be done:

  1. Modify the C# compiler to detect immutable collections and use the static field to construct them, rather than a parameterless constructor.
  2. Modify the C# deserializer to specially handle reconstructing immutable collections, either using their builders as an intermediary, using the specific Add methods for the BT_LIST/BT_SET objects, and the SetItem method for the BT_MAP objects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants