Skip to content

enum: Underlying type #8028

Closed
Closed

Description

Enumerations in PowerShell appear to be limited to Int32. Ideally enums should allow inheritance-style setting of the underlying type.

Steps to reproduce

PowerShell does not appear to allow an underlying type to be defined for an enum. All values are coerced to Int32.

The statement below will raise an exception:

enum Fruit { Apple = 0x0FFFFFFFFFFFFFFF }

Expected behavior

An enumeration wiith underlying type set to [Int64] to be created.

enum Fruit { Apple = 0x0FFFFFFFFFFFFFFF }
[Fruit].GetEnumUnderlyingType() -eq [Int64]

Actual behavior

An exception is throw when the attempt to cast fails.

Enumerator value is too large for a System.Int.

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.1.0
PSEdition                      Core
GitCommitId                    6.1.0
OS                             Microsoft Windows 10.0.17134
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. vexx32 commented on Oct 15, 2018

    @vexx32
    Collaborator

    I'd recommend reusing the class inheritance syntax, which would be similar to C# syntax for specifying an enum type:

    enum MyEnum : Int64 {
        Val1
        Val2
        Val3
    }
    
  2. added
    Issue-Enhancement
    WG-Engine
    on Oct 16, 2018
  3. iSazonov commented on Oct 16, 2018

    @iSazonov
    Collaborator

    Rob Holt (@rjmholt) For your classes collection. :-)

  4. rjmholt commented on Oct 16, 2018

    @rjmholt
    Collaborator

    I suspect this was a deliberate design decision when enums were introduced. How clean was the error message Chris Dent (@indented-automation)?

  5. vexx32 commented on Oct 16, 2018

    @vexx32
    Collaborator

    Looks like this Rob Holt (@rjmholt)

    At line:2 char:8
    + Val1 = 0x7FFFFFFFFFFFFFFF
    +        ~~~~~~~~~~~~~~~~~~
    Enumerator value is too large for a System.Int.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EnumeratorValueTooLarge
    

    And it may have been a deliberate decision, but given that class inheritance is a thing anyway, I don't see any particular reason to arbitrarily restrict enums in this fashion?

  6. rjmholt commented on Oct 16, 2018

    @rjmholt
    Collaborator

    Yes I just tested it myself. Agreed that there's no particular reason to not support it other than because it makes the language more complicated. I suspect the original reasoning was that smaller enum values work already (and can be cast without effort) and larger enum values are a pretty obscure use case, especially when flag usage is not a primary use case.

  7. vexx32 commented on Oct 16, 2018

    @vexx32
    Collaborator

    That's fair, I suppose, but [Flags()] is a supported enum attribute, so it makes the most sense to provide the frameworks that might be necessary to use it, even if the overall need isn't immensely huge. 😄

    (also, the inconsistency just bothers me somehow that class inheritance syntax exists but can't be used for enum specifically, idk)

  8. rjmholt commented on Oct 16, 2018

    @rjmholt
    Collaborator

    Yeah there might be the odd .NET API out there that requires it maybe. The inheritance thing is more an expectation set by C#'s syntactic sugar -- enums can't really "inherit" (playing with the metadata, it's diabolical trying to find out what the underlying type of an enum is -- you need to look at the value__ field).

  9. added
    Issue-Discussion
    and removed
    Issue-Enhancement
    on Oct 17, 2018
  10. added a commit that references this issue on Jan 17, 2019
    59a3696
  11. msftrncs commented on Jan 23, 2019

    @msftrncs
    Contributor

    I just wanted to comment that sometimes its not about larger values, but smaller ones, and smaller storage space. This is more important if an enum was packed in to a structure, which is more common with small controller platforms than PC applications. If an enum only needs a byte (8 bits) to express all its values, why should it consume any further space.

    Regarding determining the type, the enum class offers 'getUnderlyingType' or 'getEnumUnderlyingType'.

    Its nice to see this was just added. I'll add it to the syntax I have been working on for PowerShell/EditorSyntax.

  12. rjmholt commented on Jan 23, 2019

    @rjmholt
    Collaborator

    Struct packing is the kind of thing that's better suited to C#. I'm personally wary of turning PowerShell into C#, since it'll never be better than C# at such things, but it's much better/more ergonomic than C# at other things. And maintaining all the different features doesn't come for free.

    Regarding determining the type, the enum class offers 'getUnderlyingType' or 'getEnumUnderlyingType'.

    Once you've loaded it you get methods, yes. But I meant looking at the DLL binary metadata -- enums aren't just their backing word type in IL.

  13. SwissPowershell commented on Feb 3, 2025

    @SwissPowershell

    for whom that had this problem I workarounded using the Add-Type and creating a long Enum :

        Add-Type @"
        public enum GroupType : long {
            GROUP_TYPE_BUILTIN_LOCAL_GROUP = 0x00000001,
            GROUP_TYPE_ACCOUNT_GROUP = 0x00000002,
            GROUP_TYPE_RESOURCE_GROUP = 0x00000004,
            GROUP_TYPE_UNIVERSAL_GROUP = 0x00000008,
            GROUP_TYPE_APP_BASIC_GROUP = 0x00000010,
            GROUP_TYPE_APP_QUERY_GROUP = 0x00000020,
            GROUP_TYPE_SECURITY_ENABLED = 0x80000000
        }
        "@
    
  14. added a commit that references this issue on Aug 6, 2026
    450c509
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

    Issue-DiscussionResolution-FixedWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions