Skip to content

Commit 2287ae0

Browse files
committed
c# codegen: revert to always using List<T> for arrays
1 parent 58066ef commit 2287ae0

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

crates/cli/src/subcommands/generate/csharp.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ fn ty_fmt<'a>(ctx: &'a GenCtx, ty: &'a AlgebraicType, namespace: &'a str) -> imp
5555
}
5656
// Arbitrary product types should fail.
5757
AlgebraicType::Product(_) => unimplemented!(),
58-
AlgebraicType::Array(ArrayType { elem_ty }) => write!(f, "{}[]", ty_fmt(ctx, elem_ty, namespace)),
58+
AlgebraicType::Array(ArrayType { elem_ty }) => {
59+
write!(
60+
f,
61+
"System.Collections.Generic.List<{}>",
62+
ty_fmt(ctx, elem_ty, namespace)
63+
)
64+
}
5965
AlgebraicType::Ref(r) => {
6066
let name = csharp_typename(ctx, *r);
6167
match &ctx.typespace[*r] {
@@ -82,18 +88,17 @@ fn ty_fmt<'a>(ctx: &'a GenCtx, ty: &'a AlgebraicType, namespace: &'a str) -> imp
8288
})
8389
}
8490

85-
fn default_init(ctx: &GenCtx, ty: &AlgebraicType, namespace: &str) -> Option<String> {
91+
fn default_init(ctx: &GenCtx, ty: &AlgebraicType) -> Option<&'static str> {
8692
match ty {
8793
// Options have a default value of null which is fine for us, and simple enums have their own default.
8894
AlgebraicType::Sum(sum_type) if sum_type.is_option() || sum_type.is_simple_enum() => None,
8995
// TODO: generate some proper default here (what would it be for tagged enums?).
90-
AlgebraicType::Sum(_) => Some("null!".into()),
91-
AlgebraicType::Array(arr_ty) => Some(format!("Array.Empty<{}>()", ty_fmt(ctx, &arr_ty.elem_ty, namespace))),
92-
// For product types, we can use the default constructor.
93-
AlgebraicType::Product(_) => Some("new()".into()),
96+
AlgebraicType::Sum(_) => Some("null!"),
97+
// For product types and arrays, we can use the default constructor.
98+
AlgebraicType::Product(_) | AlgebraicType::Array(_) => Some("new()"),
9499
// Strings must have explicit default value of "".
95-
AlgebraicType::String => Some(r#""""#.into()),
96-
AlgebraicType::Ref(r) => default_init(ctx, &ctx.typespace[*r], namespace),
100+
AlgebraicType::String => Some(r#""""#),
101+
AlgebraicType::Ref(r) => default_init(ctx, &ctx.typespace[*r]),
97102
_ => {
98103
debug_assert!(ty.is_scalar());
99104
None
@@ -342,7 +347,7 @@ fn autogen_csharp_product_table_common(
342347
writeln!(output, "public {name}()");
343348
indented_block(output, |output| {
344349
for ((field_name, _ty), field) in fields.iter().zip(&*product_type.elements) {
345-
if let Some(default) = default_init(ctx, &field.algebraic_type, namespace) {
350+
if let Some(default) = default_init(ctx, &field.algebraic_type) {
346351
writeln!(output, "this.{field_name} = {default};");
347352
}
348353
}
@@ -524,7 +529,7 @@ pub fn autogen_csharp_reducer(ctx: &GenCtx, reducer: &ReducerDef, namespace: &st
524529

525530
write!(output, "public {arg_type_str} {field_name}");
526531
// Skip default initializer if it's the same as the implicit default.
527-
if let Some(default) = default_init(ctx, &arg.algebraic_type, namespace) {
532+
if let Some(default) = default_init(ctx, &arg.algebraic_type) {
528533
write!(output, " = {default}");
529534
}
530535
writeln!(output, ";");

0 commit comments

Comments
 (0)