Open IISResetMe opened 2 years ago
I think the key aspects of a powershell Record that should be preserved from C# should be:
These are the features I think would make it most useful, give Powershell users a model for data that is easily validatable and transportable between processes.
This task is to explore, design and prototype proposals for:
struct
)record
, either as a modifier or separate type definition)Value types
PowerShell's type definer only supports emission of reference types:
class
keyword emits a CTS-compliant class type with the following non-configurable constraints/defaults (consistent with CSC/Roslyn defaults):AutoLayout, AnsiClass
enum
keyword emit a CTS-compliant public, non-static, sealed enumeration type (entirely consistent with CSC/Roslyn)These constraints limit the usefulness of PSType definitions in interop scenarios.
The usefulness of value types do have some detractors in the context of PowerShell, since ETS relies on identity (ie. reference equality) for PSObject resurrection, but since value type creation is trivial to implement both syntax and compilation for (it's basically a subset of existing functionality with a few additional .ctor constraints), this topic should definitely be explored further.
Record types
Record types, introduced in C# 9, are a syntax feature that causes the compiler to generate default behaviors that make instances behave like value types even though they're not.
The following type definition:
will cause generation of IL closer resembling:
This means you get by-val like equality comparison but retain identity on assignment:
We don't necessarily need to cargo-cult this exact set of behaviors, but the default implementation of
IEquatable<T>
on reference types makes a lot of sense for data structures in PowerShell.Outstanding items:
record
keyword)