I propose we allow this:
let zero (span : System.Span<int>) =
for item : int byref in span do item <- 0
let useInref (span : System.ReadOnlySpan<int>) =
for item : int inref in span do System.ReadOnlySpan (&item)
This is the equivalent of C#'s
If the enumerator's Current property returns a reference return value (ref T where T is the type of a collection element), you can declare an iteration variable with the ref or ref readonly modifier
Since byref/inref/outref cannot be an inferred type from type inference, this should not be a breaking change to type inference.
The existing way of approaching this problem in F# is to work around the absence of this feature entirely, for example using integer iterators, which is verbose and not applicable to collections that are not randomly indexable. Alternatively, writing manual while loops with direct invocation of Current and MoveNext which is more error-prone than for loops.
Pros and Cons
The advantages of making this adjustment to F# are
- C# parity
- Conciseness
- Encouraging writing performant code that is safe
The disadvantage of making this adjustment to F# is implementation complexity.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Related suggestions: (put links to related suggestions here)
Affidavit (please submit!)
Please tick these items by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
I propose we allow this:
This is the equivalent of C#'s
Since
byref/inref/outrefcannot be an inferred type from type inference, this should not be a breaking change to type inference.The existing way of approaching this problem in F# is to work around the absence of this feature entirely, for example using integer iterators, which is verbose and not applicable to collections that are not randomly indexable. Alternatively, writing manual
whileloops with direct invocation ofCurrentandMoveNextwhich is more error-prone thanforloops.Pros and Cons
The advantages of making this adjustment to F# are
The disadvantage of making this adjustment to F# is implementation complexity.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Related suggestions: (put links to related suggestions here)
Affidavit (please submit!)
Please tick these items by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.