Repository navigation
Closed
Labels
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime
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.0Reacted by Rain Sallow (/u/ta11ow), Michael Klement, Mathias R. Jessen and Kris Borowinski
Reacted by Rain Sallow (/u/ta11ow)
Metadata
Metadata
Assignees
Labels
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime
Activity
vexx32 commentedon Oct 15, 2018
I'd recommend reusing the class inheritance syntax, which would be similar to C# syntax for specifying an enum type:
iSazonov commentedon Oct 16, 2018
Rob Holt (@rjmholt) For your classes collection. :-)
rjmholt commentedon Oct 16, 2018
I suspect this was a deliberate design decision when
enums were introduced. How clean was the error message Chris Dent (@indented-automation)?vexx32 commentedon Oct 16, 2018
Looks like this Rob Holt (@rjmholt)
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?
rjmholt commentedon Oct 16, 2018
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.
vexx32 commentedon Oct 16, 2018
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
enumspecifically, idk)rjmholt commentedon Oct 16, 2018
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 anenumis -- you need to look at thevalue__field).Allow user-specified underlying type for enums (#8329)
msftrncs commentedon Jan 23, 2019
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.
rjmholt commentedon Jan 23, 2019
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.
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.
SwissPowershell commentedon Feb 3, 2025
for whom that had this problem I workarounded using the Add-Type and creating a long Enum :
Allow user-specified underlying type for enums (PowerShell#8329)