Skip to content

Strange behavior in runtime #18417

Open
Open
@ingted

Description

@ingted

According to this #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".

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Compiler-SRTPbugs in SRTP inference, resolution, witness passing, code genBugImpact-Low(Internal MS Team use only) Describes an issue with limited impact on existing code.

    Type

    No type

    Projects

    Status

    New

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions