Skip to content

Generic struct type whose fields are all unmanaged types is unmanaged #692

@dzmitry-lahoda

Description

@dzmitry-lahoda

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:
dotnet/csharplang#1504
dotnet/csharplang#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
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • 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:

  • 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions