Automatic implementation of IBinaryInteger<self> for enums
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Start with issue #112819 and investigate how enum types, IBinaryInteger, and compiler/runtime capability checks are currently handled. Determine the design needed for automatic support, including formatting, parsing, and numeric conversions. Done means the behavior is specified and implemented with coverage for supported and unsupported target runtimes.
Written by the indexing model from the issue text.
Description
There's only one thing holding back code such as the below from working right now, and it's that enums do not implement IBinaryInteger<self>:
static T ConsumeEnumValue<T>(ref ReadOnlySpan<byte> buffer, bool isUnsigned) where T : unmanaged, Enum, IBinaryInteger<T>
{
var value = T.ReadLittleEndian(buffer, isUnsigned);
unsafe { buffer = buffer[sizeof(T)..]; }
return value;
}
Has automatic implementation of this interface this been considered, similar to how the runtime implements IReadOnlyList<T> on T[]? IBinaryInteger<T> for .NET enums seems like perfect fit. It would enable generic specialization without writing out a lot of manual reflection code to detect and handle each underlying int size separately. This would also enable enums to participate in all existing generic numerics code.
The compiler would have to detect the runtime capability similarly to how it detects whether the targeted runtime supports native integers. If the targeted runtime does not support it, then it would be a compile error to use a concrete enum type as the type argument to a type parameter constrained : Enum, IBinaryInteger<T>.
It would probably not be desirable for an emitted enum type to explicitly implement this interface in general because the implementation would generally be exactly the same per underlying integer type, and that could be a lot of metadata repetition.
This would only apply to enums whose underlying type implements IBinaryInteger<T>.
Most of the implementation would simply be reusing the IBinaryInteger<T> implementation for the underlying integer type. The exceptions would be:
- Formatting and parsing to and from chars, which would unify with existing enum formatting and parsing logic.
- TryConvertTo/TryConvertFrom implementations would allow converting to and from the underlying integer type, so that
ulong.CreateTruncating(anyEnumValue)could work and vice versa.
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/runtime
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
area-System.Reflection blocking-clean-ci-optional Known Build Error os-mac-os-x untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
area-CodeGen-coreclr untriaged
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
agentic-workflows untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
area-VM-meta-mono untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
Create parent directories only after the containment check in InstallHelper.TryExtractToDirectory Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
PowerShell/PSResourceGet#2056 ·