fslang-suggestions icon indicating copy to clipboard operation
fslang-suggestions copied to clipboard

Generic struct type whose fields are all unmanaged types is unmanaged

Open dzmitry-lahoda opened this issue 7 years ago • 7 comments

Generic struct type whose fields are all unmanaged types is unmanaged

A non-generic struct type whose fields are all unmanaged types is unmanaged.

into

A generic struct type whose fields are all unmanaged types is unmanaged.

as

The unmanaged constraint on generic type T enforces the type to be an 'unmanaged' struct that does not recursively contain reference type fields.

as for cases

type Cell<'T when 'T: unmanaged> =
    struct
        val element: 'T
        val next: nativeptr<Cell<'T>>
    end    

type UnmanagedCell<'T when 'T: unmanaged> =
    struct
    end  

[<EntryPoint>]
let main argv =
    let fine = UnmanagedCell<int>()
    let FS0001 = UnmanagedCell<UnmanagedCell<int>>()
    0

The existing way of approaching this problem in F# is prevent compilation with error

error FS0001: A generic construct requires that the type 'X' is an unmanaged type

I have asked the question on SO and found that it is F# limitation, not CLR.

Pros and Cons

See Related suggestions

Extra information

Related suggestions: https://github.com/dotnet/csharplang/issues/1504 https://github.com/dotnet/csharplang/issues/1649

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • [x ] This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • [x] I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • [x] This is not a breaking change to the F# language design
  • [ ] I or my company would be willing to help implement and/or test this

dzmitry-lahoda avatar Sep 10 '18 15:09 dzmitry-lahoda

This is actually quite easy to implement and it should not be a breaking change. We should make this change to follow C#.

TIHan avatar Jan 05 '19 01:01 TIHan

We would also need to interop with C# by looking at the IsUnmanagedAttribute on the type argument and be able to emit it as well.

TIHan avatar Jan 05 '19 01:01 TIHan

For reference, here's a link to the original PR for C#, which is now merged: https://github.com/dotnet/roslyn/pull/31148.

Edit: apparently more work was done on several PR's, here's all: https://github.com/dotnet/roslyn/commits/features/unmanaged-constructed-types.

abelbraaksma avatar Jan 05 '19 12:01 abelbraaksma

@TIHan as a part of this we should also update the Span 'n Friends RFC

cartermp avatar Jan 05 '19 14:01 cartermp

Yes and we would need to update docs as well.

TIHan avatar Jan 06 '19 00:01 TIHan

I have a PR for this: https://github.com/Microsoft/visualfsharp/pull/6064

TIHan avatar Jan 06 '19 04:01 TIHan

This was done via https://github.com/dotnet/fsharp/pull/12154, this suggestion can be closed.

T-Gro avatar Sep 19 '24 08:09 T-Gro