clockworklabs / clockworklabs/SpacetimeDB
C#/Unity SDK - Make lists in generated client types frozen
@rekhoff is already working on this.
Since Jul 7, 2025.
- Dominant language
- Rust
- Stars
- 25.2k
- Forks
- 1.1k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 46
Description
Currently, rows containing lists generate code like:
[SpacetimeDB.Type]
[DataContract]
public sealed partial class Message
{
[DataMember(Name = "sender")]
public SpacetimeDB.Identity Sender;
[DataMember(Name = "sent")]
public SpacetimeDB.Timestamp Sent;
[DataMember(Name = "text")]
public List<char> Text;
public Message(
SpacetimeDB.Identity Sender,
SpacetimeDB.Timestamp Sent,
List<char> Text
)
{
this.Sender = Sender;
this.Sent = Sent;
this.Text = Text;
}
public Message()
{
this.Text = new();
}
}
We would like to make all of these fields readonly, but this does not suffice for preventing mutation, because the Text field can be modified even if it can't be reassigned.
To fix this, we would like to make Text non-modifiable, but how? We can't easily use List.AsReadOnly here, because it:
- Makes an extra allocation
- Returns an object of a type not matching the original type,
List<T>.
We could instead use a custom subclass of List<T> that throws exceptions on modification attempts, while still being type-compatible with List.
We'll need to make sure to instantiate this subclass anywhere the Text field is assigned to. This includes code generated by Roslyn and by the CLI:
- Roslyn: https://github.com/clockworklabs/SpacetimeDB/tree/master/crates/bindings-csharp/BSATN.Codegen
- The CLI: https://github.com/clockworklabs/SpacetimeDB/blob/master/crates/codegen/src/csharp.rs
This is low-priority, but it could conceivably happen in user code, so we'll want to keep tabs on it.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.