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

[API Proposal]: Enum validation #111018

Closed
lonix1 opened this issue Jan 1, 2025 · 4 comments
Closed

[API Proposal]: Enum validation #111018

lonix1 opened this issue Jan 1, 2025 · 4 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime

Comments

@lonix1
Copy link

lonix1 commented Jan 1, 2025

Background and motivation

There is a large assortment of input validation methods, e.g. ArgumentNullException.ThrowIfNull, ArgumentOutOfRange.ThrowIfNegative, etc.

Most fundamental use cases are covered, except for enum validation.

API Proposal

namespace System;

public class ArgumentOutOfRangeException : ArgumentException
{

  public static void ThrowIfEnumNotDefined<TEnum>(TEnum value, string? paramName = null) where TEnum : struct, Enum
  {
    if (!Enum.IsDefined(value))
      throw new ArgumentOutOfRangeException(paramName);
  }

}

API Usage

public enum States { Unknown, Off, On }

public void Foo(States state)
{
  ArgumentOutOfRangeException.ThrowIfEnumNotDefined(state, nameof(state));
  // ...
}

Alternative Designs

n/a

Risks

n/a

Thanks for considering it!

@lonix1 lonix1 added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jan 1, 2025
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jan 1, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 1, 2025
@KalleOlaviNiemitalo
Copy link

Put it in InvalidEnumArgumentException instead?

@KalleOlaviNiemitalo
Copy link

KalleOlaviNiemitalo commented Jan 1, 2025

For InvalidEnumArgumentException, it was previously rejected in #83107 and #92071 because Enum.IsDefined is slower than switch, and because a library author could add a new value to an enum type and Enum.IsDefined would then recognise it but the code that calls ThrowIfNotDefined might not handle the value correctly. These reasons also apply to adding a similar method to ArgumentOutOfRangeException.

@jkotas jkotas added area-System.Runtime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jan 1, 2025
@lonix1 lonix1 closed this as completed Jan 1, 2025
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Jan 1, 2025
@KalleOlaviNiemitalo
Copy link

The performance might be fixable by making the JIT and Native AOT compilers generate dedicated code for each enum type. This would not solve the application compatibility with new enum values, though.

A source generator (perhaps with an interceptor) could be another option. If the source were generated in an application that uses an enum type defined in a library, then the set of recognised values would depend on which version of the library was referenced at build time, rather than the version used at run time. I'm not sure that would be an improvement.

@lonix1
Copy link
Author

lonix1 commented Jan 1, 2025

Yes it's a tricky situation. The simplest solution is "to do nothing" as mentioned in that other thread.

i.e. manual validation of specific values, or if one is sure of the consequences, to use Enum.IsDefined for use within a single assembly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime
Projects
None yet
Development

No branches or pull requests

3 participants