Is there defined ordering in multiple declarations of partial record? #4957
-
As a comparison, having fields in multiple declarations of struct results in a warning CS0282. partial struct Struct // Warning: CS0282, There is no defined ordering between fields in multiple declarations of partial struct
{
public int X;
}
partial struct Struct
{
public int Y;
} Don't records need the same type of warning for their properties? Ordering of properties in records has a visible effect on the synthesized methods such as // in the same file
partial record class Record
{
public int X { get; init; }
}
partial record class Record
{
public int Y => X * X;
}
// WriteLine(new Record { X = 2 });
// Record { X = 2, Y = 4 } // in the same file
partial record class Record
{
public int Y => X * X;
}
partial record class Record
{
public int X { get; init; }
}
// WriteLine(new Record { X = 2 });
// Record { Y = 4, X = 2 } // A.cs
partial record class Record
{
public int X { get; init; }
}
// B.cs
partial record class Record
{
public int Y => X * X;
}
// WriteLine(new Record { X = 2 });
// Record { X = 2, Y = 4 } // A.cs
partial record class Record
{
public int Y => X * X;
}
// B.cs
partial record class Record
{
public int X { get; init; }
}
// WriteLine(new Record { X = 2 });
// Record { Y = 4, X = 2 } Should this behavior be defined in the spec, especially for cross-file partial declarations? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It seems like this warning is specific to structs since it more frequently interacts with issues of field layout in memory. We do give this warning with record structs. SharpLab. I think it's probably fine to explicitly say in the spec that an implementation-defined ordering is used when determining the order in which to call printable members in ToString for record classes and record structs, for example. But it doesn't seem worthy of a warning to me. cc @jcouv for visibility. |
Beta Was this translation helpful? Give feedback.
It seems like this warning is specific to structs since it more frequently interacts with issues of field layout in memory. We do give this warning with record structs. SharpLab.
I think it's probably fine to explicitly say in the spec that an implementation-defined ordering is used when determining the order in which to call printable members in ToString for record classes and record structs, for example. But it doesn't seem worthy of a warning to me.
cc @jcouv for visibility.