Background and motivation
As part of supporting Unions feature, we need:
- a new attribute that will be used by C# compiler or by a C# developer to designate a
Union type.
- a new interface that will be implemented by compiler generated or user defined
Union types.
API Proposal
namespace System.Runtime.CompilerServices
{
[AttributeUsage(Class | Struct, AllowMultiple = false)]
public class UnionAttribute : Attribute;
public interface IUnion
{
// The value of the union or null
object? Value { get; }
}
}
API Usage
public union Pet(Cat, Dog){ ... } // C# compiler generates a struct implementing IUnion with UnionAttribute on the struct
[Union] public struct Pet : IUnion // Developer manually declares a union type
{
public Pet(Cat value) => Value = value;
public Pet(Dog value) => Value = value;
public object? Value { get; }
...
}
Alternative Designs
No response
Risks
No response
Background and motivation
As part of supporting Unions feature, we need:
Uniontype.Uniontypes.API Proposal
API Usage
Alternative Designs
No response
Risks
No response