Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# feature sync: extension types #542

Open
43 tasks
SunnieShine opened this issue Feb 19, 2024 · 0 comments
Open
43 tasks

C# feature sync: extension types #542

SunnieShine opened this issue Feb 19, 2024 · 0 comments
Assignees
Labels
Area-API The issue is bound with APIs. C# feature sync The issue is bound with C#. Kind-feature request The issue is about the feature request.
Milestone

Comments

@SunnieShine
Copy link
Owner

SunnieShine commented Feb 19, 2024

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 implicit 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.
    • Exception types
      • Helper methods
    • Generic type
      • explicit operator: in T -> T*
@SunnieShine SunnieShine added Kind-feature request The issue is about the feature request. Area-API The issue is bound with APIs. labels Feb 19, 2024
@SunnieShine SunnieShine self-assigned this Feb 19, 2024
@SunnieShine SunnieShine added this to the Long Lead milestone Feb 25, 2024
@SunnieShine SunnieShine changed the title Feature sync: C# 13 extension C# 13 Feature sync: extension types Mar 9, 2024
@SunnieShine SunnieShine changed the title C# 13 Feature sync: extension types C# 13 feature sync: extension types Mar 9, 2024
@SunnieShine SunnieShine changed the title C# 13 feature sync: extension types C# feature sync: extension types Apr 21, 2024
@SunnieShine SunnieShine modified the milestones: Version x.x, Version 3.x May 23, 2024
@SunnieShine SunnieShine added the C# feature sync The issue is bound with C#. label Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-API The issue is bound with APIs. C# feature sync The issue is bound with C#. Kind-feature request The issue is about the feature request.
Projects
None yet
Development

No branches or pull requests

1 participant