Skip to content

Conversation

rekhoff
Copy link
Contributor

@rekhoff rekhoff commented Sep 8, 2025

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 type

Because 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 return BSATN0001 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

  • Regression test of functionality added.

@rekhoff rekhoff self-assigned this Sep 8, 2025
@jdetter jdetter changed the title Rekhoff/csharp default feild values Rekhoff/csharp default field values Sep 12, 2025
@rekhoff rekhoff requested a review from jdetter September 19, 2025 14:58
Copy link
Contributor

Some missing or dangling .meta found. Fix commits are needed.

Status File
Not committed global.json.meta

Copy link
Contributor

Some missing or dangling .meta found. Fix commits are needed.

Status File
Not committed global.json.meta

@rekhoff rekhoff marked this pull request as ready for review October 2, 2025 01:09
Copy link
Collaborator

@jdetter jdetter left a 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 👍

@rekhoff rekhoff added this pull request to the merge queue Oct 6, 2025
@bfops bfops added the release-any To be landed in any release window label Oct 6, 2025
Merged via the queue into master with commit a546bc8 Oct 6, 2025
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-any To be landed in any release window

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants