dotnet / dotnet/fsharp

Strange behavior in runtime

Open
#18,417 4 comments 0 reactions 0 assignees View on GitHub
Area-Compiler-SRTP Bug Impact-Low
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

According to this https://github.com/dotnet/fsharp/issues/15135

```
type MyType<^T when ^T: (member o: int)> (t:^T) =
member inline this.T = t
```

Looks good in editor (no syntax error), but results
```
error FS1113: The value 'T' was marked inline but its implementation makes use of an internal or private function which is not sufficiently accessible
```

Is normal.

However

```
type MyType1<^T when ^T: (member o:int)> = {
t: ^T
}
with
member inline this.T = this.t
```
Provides very similar functionality, but working...
```
type MyType1<^T when ^T: (member o: int)> =
{ t: ^T }
member inline T: ^T

>
```

Excuse me, is anyone kind to tell what's the difference? Since according to the past investigation,

```
I found the underlying reason. The CLR that handles generics recognizes a number of [constraints](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters), including type/interface constraints, but not explicit member constraints. The latter is a pure [F# thing](https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/generics/constraints). This means (now I am speculating) the compiler has to express it in IL by an interface constraint, and that can be done only if MyType is a "head type", i.e. statically known at compile time. These complications are also the reason why the F# Language Reference says that explicit member constraints are "not intended for common use".
```

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.