-
Notifications
You must be signed in to change notification settings - Fork 796
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
Operators.typeof<'T> does not seems work with IsByRefLike types #6607
Comments
This is considered by design as |
@cartermp How am I supposed to have type information about IsByRefLike types in F# then? How the writing a helper method for every type typeof in C# solve the problem? How am we supposed to explain to the auditors the F# language choice pros when we have to use C# for these basic functionality? How the C# typeof operator could work without generic? |
This scenario isn't supported right now. We may consider some kind of special case here in the future, but
|
@zpodlovics, I think this is something that needs to be discussed over in the language design forum. It |
I'd recommend using |
Found a workaround using type aliases. The IsByRefLike types still cannot be used for type generics as the type loading will fail with TypeLoadException. Please do NOT remove this functionality until different (non-generic?) operator provide the same instruction sequence and functionality for IsByRefLike types (not only for span but als for custom IsByRefLike types too) too. The number of generated IL / ASM instructions does matter for performance critical cases. Maybe inlined and NoDynamicInvocation marked functions could use IsByRefLike types as type parameters. open System
open System.Runtime.CompilerServices
type SpanAlias<'T> = System.Span<'T>
type [<Struct;IsByRefLike>] S =
val Value: int32
new(v:int32) = { Value = v }
type SAlias = S
type ByRefGeneric<'T> =
val Value: 'T
new(v:'T) = { Value=v }
[<EntryPoint>]
let main argv =
//Note: this will cause the following error:
//Unhandled Exception: System.TypeLoadException: The generic type 'ByRefGeneric`1' was used with an invalid instantiation in assembly 'typeofspan, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
//let x = ByRefGeneric<SpanAlias<byte>>(Span<byte>())
let t = typeof<SpanAlias<byte>>
let t2 = typeof<SAlias>
printfn "Span.IsValueType: %b" (t.IsValueType)
printfn "S.IsValueType: %b" (t2.IsValueType)
0 // return an integer exit code This generate the same instruction sequence as C#: Span typeof:
S typeof:
C# |
@zpodlovics We have no plans to disable aliasing like that. There is a bug where you can use an alias to get around the inability to store byref-like types in reference types (#6133) but this doesn't affect your workaround. |
I am hitting this limitation and I can't seem to make the alias trick work with the compiler from SDK 6.0.100. The following code: module Test
type MySpan<'T> = System.Span<'T>
let x = typeof<MySpan<int>> yields this error on the
|
It seems that typeof operator does not seems work with IsByRefLike types.
Repro steps
Provide the steps required to reproduce the problem
typeofspan-csharp.zip
typeofspan-fsharp.zip
Expected behavior
The typeof operator works with IsByRefLike types.
Actual behavior
The typeof operator does not seems works with IsByRefLike types.
Known workarounds
None.
Related information
The text was updated successfully, but these errors were encountered: