Skip to content

[API Proposal]: Enum.HasFlag<TEnum>(TEnum flag) #66261

Closed

Description

@SupinePandora43

Background and motivation

A better editor intellisense

API Proposal

namespace System
{
    public static class EnumExtensions
    {
        public static bool HasFlag<TEnum>(this TEnum en, TEnum flag) where TEnum : System.Enum => en.HasFlag(flag);
    }
}

API Usage

[Flags]
enum E {
    A = 1,
    B = 2,
    C = 4
}

E e = E.A | E.C; // retrieve Enum
bool hasFlag = e.HasFlag<E>(E.B);

Alternative Designs

Code-generated flag get/set-ters with a requirement of JIT to optimize HasFlag(E.A) && HasFlag(E.B) to HasFlag(E.A | E.B)

Risks

Performance (at least bigger asm/il code https://sharplab.io/#v2:EYLgtghglgdgNAFxBAzmAPgAQEwEYCwAUJgAwAEmuAdAEoCuMCUYAplQMID2YADlADYsATgGVhANygBjFigDcRANoAxfhADmKALpFMAZjIsYdMGRQIA3viJlbd2wEEyAXlxwyAIRfZ37ZwBYbezsAX10DSgA2CmwyAFFjMDiAD0sg23SyRREACwghHgAZCGBaBiZWKgApKAQAcSNhaQAKBABPHhZOADNm8wBKfp1Ce0UAWRYEHM4AEwBJXn5mianZhZ5+AHkeJk4YFCoHdXUhWRQocRY5mH5YWHUh8IpcaOBOTn4yAAlUVQ0AHgAKgkTAA+Vo5KAoMjAxJkRLuWEmMjdNQPMgAdxywhYMJBphAzyo+KsIzsmAA7PCTFQfig/upmqiNP0FIQwmTbBz9DEyOwyKT7DzzGRxGyhRF/GQxs1+gLMkLcABOZriWm/NF9BBUDyDcXBMgciUUKV1WWCg3PFVqukM/7mcHmHV6zIckJAA=== )

Activity

  1. added
    api-suggestion
    on Mar 6, 2022
  2. ghost added
    untriaged
    on Mar 6, 2022
  3. EgorBo commented on Mar 6, 2022

    @EgorBo
    Member

    IMO it's not clear from the proposal what this API is supposed to solve/help with.

  4. SupinePandora43 commented on Mar 6, 2022

    @SupinePandora43
    Author

    IMO it's not clear from the proposal what this API is supposed to solve/help with.

    TEnum.HasFlag(Enum) can be called with any Enum , not just TEnum.

  5. teo-tsirpanis commented on Mar 6, 2022

    @teo-tsirpanis
    Contributor

    There could be an analyzer that warns about invalid usage of the non-generic HasFlag.

  6. tannergooding commented on Mar 14, 2022

    @tannergooding
    Member

    CC. @jkotas, are there any concerns on this one or with exposing generic variants of "most" static methods on System.Enum (outside the cost of specializing them in the runtime)?

    It does seem like an analyzer could help cover a number of these scenarios and may be a better solution than exposing a generic variant of every method.

  7. jkotas commented on Mar 14, 2022

    @jkotas
    Member

    My opinion is that it is best to use the built-in bit operators for [Flags] enum operations. It is typically less characters, guaranteed to be fast and gives you full expressivity. I do not see a value in re-recreating longer named equivalents.

    TEnum.HasFlag(Enum) can be called with any Enum , not just TEnum.

    The built-in bit operators check that the enum types match already.

  8. ghost locked as resolved and limited conversation to collaborators on Apr 14, 2022
  9. removed
    untriaged
    on Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.Runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions