Description
Is your feature request related to a problem? Please describe.
By trying to remove boxing for the string
function, I got blocked by the fact that I cannot special-case enums. In fact, the static when
syntax is quite limited, and probably rightly so, but I would like to see if it is possible to extend this with support for enum<_>
, so that we can distinguish between enums and integral types.
Describe the solution you'd like
I'd like to allow syntax like:
let myFunction (x: 'T) =
doSomething x
when 'T: enum<_> = doSomethingWithEnum
Alternatively, we can use enum
without type spec. But I figured, since normal generic type restrictions are possible with enum<'T>
where 'T
is an integral type, this may be easier to add to the language.
Like with the normal enum type restriction, after the equals-sign, x
has type annotation requires enum
.
Describe alternatives you've considered
I don't think there's an alternative that allows different code paths to be statically resolved for integral types (that are not enum) and enums (of an integral type).
Additional context
This restriction was discussed at some length in #9549. If there's another workaround, I'll gladly take it ;). Since this is an "internal to the compiler only" request, I don't think it ought to go through the language-suggestions, but I've no problem to re-post there ;).
I looked around in the compiler code where this static when
clause was resolved, but failed to find it (it's not easy to search for "when", it's omnipresent).
I think the only "special" type allowed is struct
. Others, that are allowed in generics and SRTP, like not struct
, enum<_>
, delegate<_, _>
are not available in this context.
Benefit of implementing this would be that we can write more targeted IL code for FSharp.Core functions. But whether it is worth the effort if the only case is to prevent boxing for string
remains to be seen.