-
Notifications
You must be signed in to change notification settings - Fork 613
Rekhoff/csharp default field values #3235
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some missing or dangling .meta found. Fix commits are needed.
|
Some missing or dangling .meta found. Fix commits are needed.
|
…com/clockworklabs/SpacetimeDB into rekhoff/csharp-default-feild-values
jdetter
reviewed
Oct 3, 2025
jdetter
approved these changes
Oct 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have not manually tested this but the regression tests are easy to understand and are passing. Thanks Ryan this looks great 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of Changes
This is the implementation of issue #3191. This adds a Default attribute to C# module fields.
Note: In C#, attribute arguments must be compile-time constants, which means you can't directly use non-constant expressions like new expressions, method calls, or dynamic values in attribute constructors.
(Ref: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/attributes#2324-attribute-parameter-types)
For this reason, these default values are limited to primitive types, enums, and strings.
This includes (shown as
C# Type
(BSATN type
):bool
(Bool
)sbyte
(I8
)byte
(U8
)short
(I16
)ushort
(U16
)int
(I32
)unit
(U32
)long
(I64
)ulong
(U64
)float
(F32
)double
(F64
)enum
(Enum
)string
(String
)null
(RefOption
) <- Nullable typeBecause of C# limitations, for nullable and complex data types, such as a struct, can take use
[Default(null)]
to populate values with null defaults. This allows things like structs to workaround the non-constant expressions in attribute constructors limitation by allowing these complex types to still be able to be added as new column tables.The
int
type can also be in the form of Hex or Binary literals, such as[Default(0x2A)]
or[Default(0b00101010)]
Both Decimal (like
[Default(3.14m)]
) and Char (like[Default('A')]
) are unsupported types in BSATN and will still returnBSATN0001
errors.API and ABI breaking changes
Not API breaking.
This change only adds the
[Default(value)]
attribute logic.Using the
[Default(value)]
attribute with older versions SpacetimeDB C# modules will result in an error.Expected complexity level and risk
2
Testing
Local testing of this requires use of CLI changes in #3278