Sudoku icon indicating copy to clipboard operation
Sudoku copied to clipboard

C# feature sync: `extension` types

Open KyouyamaKazusa0805 opened this issue 1 year ago • 0 comments

C# language design team wants to introduce a new feature called extension, a new type kind in C# type system.

What is the feature?

This feature will allow you extending nearly everything. In C# 3, we have a feature called "Extension Methods", which can define a method that can be invoked like a normal instance:

static void Method(this int variable)
    => Console.WriteLine(variable);

Then you can use this method, just like the type int contains this method:

variable.Method();

Begin with C# 13, the feature expands the rule, to extend nearly all member kinds of a type:

  • Extension Fields
  • Extension Properties
  • Extension Indexers
  • Extension Events
  • Extension Methods
  • Extension Operator Overloads
  • Extension Cast Operator Overloads

Although constructors cannot be extended, this feature is still "good enough" for our coding environment.

I will sync the solution, using this feature to extend all types if worth.

//
// Define
//
public extension GenericExtension<T> for T where T : allows ref struct
{
    public TResult ReinterpretCast<TResult>()
        => Unsafe.As<T, TResult>(ref this);
}

//
// Consume
//
float f = 42F;
int i = f.ReinterpretCast();

To-do list

  • [ ] Extend built-in types
    • [ ] Type object
      • [ ] ReinterpretCast<>: Cast the object to the other type without memory bits change, same as Unsafe.As<,>.
      • [ ] operator true and operator false: Determine whether a reference is not null.
    • [ ] Type string
      • [ ] operator *(string, int): Repeats the string multiple times (return a string[]).
      • [ ] operator /(string, int): Split the string to multiple sub-strings of the specified length.
      • [ ] operator /(string, char): Split the string using the specified character separator.
      • [ ] operator /(string, string): Split the string using the specified string separator.
    • [ ] Type char
      • [ ] operator *(char, int): Repeats the character multiple times.
    • [ ] Types byte, sbyte, ushort, short, uint, int, ulong, long, nint and nuint
      • [ ] this[int index]: Gets the bit at the specified index.
      • [ ] GetEnumerator: Iterates on each bit position.
      • [ ] operator true and operator false: Check whether the value is not 0.
    • [ ] Enumeration types
      • [ ] T.MinValue and T.MaxValue: Defines the minimal and maximum value.
      • [ ] T.All: Defines all flags combined for an enumeration type marked FlagsAttribute.
      • [ ] T.None: Introduces the field that holds the default value.
      • [ ] Reinterpret<TBase, TResult>: Converts the enumeration field of type TBase to TResult, with whose name is same; equivalent to Enum.Parse<TResult>(field.ToString()).
  • [ ] Extend custom types
    • [ ] Integer types sbyte, byte, short, ushort, int, uint, long, ulong, nint and nuint
      • [ ] Fallback: Equals sizeof(TInteger) << 3, meaning the return value of TrailingZeroCount(TInteger) on value equals 0
    • [ ] IEnumerable and IEnumerable<T>
      • [ ] this[int index]: Calls ElementAt.
      • [ ] Count: Calls Count().
      • [ ] Slice: Slices the collection, also be used in slice operator ...
      • [ ] operator *: Calls Aggregate.
    • [ ] IGetSubsetMethod<TSelf, TSource>:
      • [ ] operator &(in TSelf, int): Calls GetSubsetsExact.
      • [ ] operator |(in TSelf, int): Calls GetSubsetsLower.
    • [ ] List<T>
      • [ ] operator +: Calls Add.
      • [ ] operator -: Calls Remove.
      • [ ] implicit operator ReadOnlySpan<T>: Implicit cast to ReadOnlySpan<T>.
      • [ ] implicit operator Span<T>: Implicit cast to Span<T>.
    • [ ] StringBuilder
      • [ ] operator >>: Print formatted text from builder.
      • [ ] operator <<: Add item.ToString into bulider.
    • [ ] CancellationToken
      • [ ] operator true: Invokes property IsCancellationRequested
      • [ ] operator false: Equivalent to !ct.IsCancellationRequested
    • [ ] Exception types
      • [ ] Helper methods
    • [ ] Generic type
      • [ ] explicit operator: in T -> T*

KyouyamaKazusa0805 avatar Feb 19 '24 08:02 KyouyamaKazusa0805