Skip to content

FS-1053 update :: Support instantiation of C#-defined "allows ref struct" generics with ByrefLike types#787

Merged
vzarytovskii merged 1 commit intofsharp:mainfrom
T-Gro:patch-2
Nov 5, 2024
Merged

FS-1053 update :: Support instantiation of C#-defined "allows ref struct" generics with ByrefLike types#787
vzarytovskii merged 1 commit intofsharp:mainfrom
T-Gro:patch-2

Conversation

@T-Gro
Copy link
Copy Markdown
Contributor

@T-Gro T-Gro commented Aug 26, 2024

Click “Files changed” → “⋯” → “View file” for the rendered RFC.

Comment thread FSharp-4.5/FS-1053-span.md
* Cannot be captured by any closure construct (async methods or lambda expressions)
* Cannot be used as a generic parameter
- In F# 9 and higher, this restriction is relaxed if the generic parameter is defined in C# using the `allows ref struct` anti-constraint. F# can instantiate such generics in types and methods with byref-like types. As a few examples, this affects BCL delegate types (`Action<>`,`Func<>`), interfaces (`IEnumerable<>`,`IComparable<>`) and generic arguments with a user-provided accumulator function (`String.string Create<TState>(int length, TState state, SpanAction<char, TState> action)`)
- It is impossible to author generic code supporting byref-like types in F#.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth expanding on this here? E.g., to mention that the allows ref struct anti-constraint is not automatically propagated in the same way as normal generic constraints, with some examples.

let a : Action<'T> =let f (x : 'T) = a.Invoke x // Can 'T be ReadOnlySpan<int> here? Presumably not?
let f (g : Action<'T>) (x : 'T) = g.Invoke x // Can 'T be ReadOnlySpan<int> here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not automatically propagated, this makes sense to expand on - thanks for checking this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants