Sudoku
Sudoku copied to clipboard
C# feature sync: `extension` types
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 asUnsafe.As<,>
. - [ ]
operator true
andoperator false
: Determine whether a reference is notnull
.
- [ ]
- [ ] Type
string
- [ ]
operator *(string, int)
: Repeats the string multiple times (return astring[]
). - [ ]
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
andnuint
- [ ]
this[int index]
: Gets the bit at the specified index. - [ ]
GetEnumerator
: Iterates on each bit position. - [ ]
operator true
andoperator false
: Check whether the value is not 0.
- [ ]
- [ ] Enumeration types
- [ ]
T.MinValue
andT.MaxValue
: Defines the minimal and maximum value. - [ ]
T.All
: Defines all flags combined for an enumeration type markedFlagsAttribute
. - [ ]
T.None
: Introduces the field that holds the default value. - [ ]
Reinterpret<TBase, TResult>
: Converts the enumeration field of typeTBase
toTResult
, with whose name is same; equivalent toEnum.Parse<TResult>(field.ToString())
.
- [ ]
- [ ] Type
- [ ] Extend custom types
- [ ] Integer types
sbyte
,byte
,short
,ushort
,int
,uint
,long
,ulong
,nint
andnuint
- [ ]
Fallback
: Equalssizeof(TInteger) << 3
, meaning the return value ofTrailingZeroCount(TInteger)
on value equals 0
- [ ]
- [ ]
IEnumerable
andIEnumerable<T>
- [ ]
this[int index]
: CallsElementAt
. - [ ]
Count
: CallsCount()
. - [ ]
Slice
: Slices the collection, also be used in slice operator..
. - [ ]
operator *
: CallsAggregate
.
- [ ]
- [ ]
IGetSubsetMethod<TSelf, TSource>
:- [ ]
operator &(in TSelf, int)
: CallsGetSubsetsExact
. - [ ]
operator |(in TSelf, int)
: CallsGetSubsetsLower
.
- [ ]
- [ ]
List<T>
- [ ]
operator +
: CallsAdd
. - [ ]
operator -
: CallsRemove
. - [ ]
implicit operator ReadOnlySpan<T>
: Implicit cast toReadOnlySpan<T>
. - [ ]
implicit operator Span<T>
: Implicit cast toSpan<T>
.
- [ ]
- [ ]
StringBuilder
- [ ]
operator >>
: Print formatted text from builder. - [ ]
operator <<
: Additem.ToString
into bulider.
- [ ]
- [ ]
CancellationToken
- [ ]
operator true
: Invokes propertyIsCancellationRequested
- [ ]
operator false
: Equivalent to!ct.IsCancellationRequested
- [ ]
- [ ] Exception types
- [ ] Helper methods
- [ ] Generic type
- [ ]
explicit operator: in T -> T*
- [ ]
- [ ] Integer types