fsharp / fsharp/fslang-suggestions
Allow inref-argument members to satisfy SRTP constraints
- Dominant language
- No language data
- Stars
- 373
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
Transferring https://github.com/dotnet/fsharp/issues/9018
This is related to [_FS-1053: voidptr, IsReadOnly structs, inref, outref, ByRefLike structs, byref extension members_](https://github.com/fsharp/fslang-design/blob/master/FSharp-4.5/FS-1053-span.md)
In C#, you can mark the parameters of an overloaded operator as ``inref``:
```c#
public readonly struct C
{
public readonly int I {get;}
public C(int i)
{
I = i;
}
public void M()
{
var c1 = new C(1);
var c2 = new C(2);
var c3 = c1 + c2;
}
public static C operator+ (in C a, in C b)
{
return new C(a.I + b.I);
}
}
```
This operator is not directly usable in F#:
```f#
let c1 = C(1)
let c2 = C(1)
let c3 = c1 + c2;
```
>error FS0001: Type constraint mismatch. The type 'C' is not compatible with type 'inref'
error FS0043: Type constraint mismatch. The type 'C' is not compatible with type 'inref'
As a workaround, you can explicitly call ``op_Addition``:
```f#
let c3 = C.op_Addition(&c1, &c2)
```
For the first part, this is by design as things currently stand.
- `a + b` is not a method call but a call to the F# library operator for `(+)` with its SRTP constraint
- the `inref` version of the op_Addition member provided by C# does not satisfy this SRTP constraint
We could consider a language suggestion where members taking `inref` arguments can be considered to satisfy such constraints but it would need an RFC. I'm not sure of its technical feasiblity.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the linked FS-1053 design and the examples in this issue. Investigate how F# library operators and SRTP constraints resolve members with inref arguments, then determine the technical and compatibility requirements for an RFC. Done means an accepted language proposal that specifies whether and how those C# operators satisfy the constraint.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, fsharp
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100