dotnet / dotnet/fsharp

Nested reference type record values and nullable constraints

Open
#13,498 9 comments 0 reactions 1 assignee Claimed by @dsyme View on GitHub
Area-Compiler-Service Feature Improvement
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

Given the following examples I can't quite figure out what the compiler is doing here.

```fs
type InnerRecord = { Id: int }

[]
type StructRecord = { Value: obj }
// Ok
let nullable = Nullable({Value = 1 })

[]
type StructRecord2 = { Value: InnerRecord }
// A generic construct requires that the type 'StructRecord2' have a public default constructor
let nullable = Nullable({Value = { Id = 1 }})

[]
type StructType(value: obj) =
member _.Value = value
// Ok
let nullable = Nullable(StructType(1))

[]
type StructType2(value: InnerRecord) =
member _.Value = value
// A generic construct requires that the type 'StructType2' have a public default constructor
let nullable = Nullable(StructType2({ Id = 1 }))

[]
type StructType3(value: StructType) =
member _.Value = value
// Ok
let nullable = Nullable(StructType3(StructType()))

[]
type InnerStructRecord = { Id: int }

[]
type StructType4(value: InnerStructRecord) =
member _.Value = value
// Ok
let nullable = Nullable(StructType4({ Id = 1 }))
```

Why specifically is it not allowed to store a struct that has a reference type record as one of its fields? (And then secondly, if it is expected behavior the error is supremely unhelpful in figuring this out)

This issue seems related to a previous issue https://github.com/dotnet/fsharp/issues/7946#issuecomment-687266672 but the linked explanation reads like it should only apply to generic structs.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.