fsharp / fsharp/fslang-suggestions

Generic struct type whose fields are all unmanaged types is unmanaged

Open
#692 7 comments 3 reactions 0 assignees View on GitHub
approved-in-principle area: structs-byrefs-and-span
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

# 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>
end

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

[]
let main argv =
let fine = UnmanagedCell()
let FS0001 = UnmanagedCell>()
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](https://stackoverflow.com/questions/52228109/a-generic-construct-requires-that-the-type-cellt-is-an-unmanaged-type) 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](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) 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

Contributor guide

No contributing guide indexed for this repository

Research direction

No repository files, tests, or entry points are named. Start with the generic struct examples and the related C# language proposals, then determine the required F# language-design and compiler changes. Done means the proposed unmanaged generic structs compile and the invalid case receives the appropriate constraint diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
fsharp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.