fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

Support more types in simple for-loops

Open brianrourkeboll opened this issue 11 months ago • 8 comments

Description

  • Support more types in for-loops, a.k.a. "simple for-loop expressions" (§ 6.5.7 of the F# language spec) or "indexed for-loops."
    • [x] Language suggestion: https://github.com/fsharp/fslang-suggestions/issues/876
    • [ ] RFC: TODO
    • [ ] RFC discussion: TODO

Examples

// = for n in 1uy..10uy do ()
for n = 1uy to 10uy do ()

// = for n in 1L..10L do ()
for n = 1L to 10L do ()

// = for n in 10L .. -1L .. 1L do ()
for n = 10L downto 1L do ()

// = for n in 1I..10I do ()
// Note however that we still do not have optimizations for bigints in either syntax.
for n = 1I to 10I do ()

// = for n in 1.0 .. 10.0 do ()
// Note however that we still do not have optimizations for floats in either syntax.
for n = 1.0 to 10.0 do ()

// = for n in 1.0m .. 10.0m do ()
// Note however that we still do not have optimizations for decimals in either syntax.
for n = 1.0m to 10.0m do ()

Checklist

  • [x] Test cases added.
  • [x] Release notes entry updated.

brianrourkeboll avatar Feb 08 '25 21:02 brianrourkeboll

:heavy_exclamation_mark: Release notes required


:white_check_mark: Found changes and release notes in following paths:

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/9.0.300.md
LanguageFeatures.fsi docs/release-notes/.Language/preview.md

github-actions[bot] avatar Feb 08 '25 21:02 github-actions[bot]

Since this does affect the language, the new fallback branch should be guarded with a LanguageFeature flag.

This will also need same extension of the https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/loops-for-to-expression section of the docs (the "docs diff" can also be used as an RFC).

Will there be limitations for downto and unsigned numbers?

T-Gro avatar Feb 10 '25 09:02 T-Gro

Since this does affect the language, the new fallback branch should be guarded with a LanguageFeature flag.

It can also be a branch producing a recoverable error like "F# 10/preview is required". It would allow having a single branch in the checking logic, while also fail the build, effectively disallowing the new behavior in the older language versions.

auduchinok avatar Feb 10 '25 10:02 auduchinok

Since this does affect the language, the new fallback branch should be guarded with a LanguageFeature flag.

It can also be a branch producing a recoverable error like "F# 10/preview is required". It would allow having a single branch in the checking logic, while also fail the build, effectively disallowing the new behavior in the older language versions.

Indeed, checkLanguageFeatureAndRecover looks optimal here

T-Gro avatar Feb 10 '25 10:02 T-Gro

Since this does affect the language, the new fallback branch should be guarded with a LanguageFeature flag.

Agreed.

This will also need same extension of the https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/loops-for-to-expression section of the docs (the "docs diff" can also be used as an RFC).

Agreed.

Will there be limitations for downto and unsigned numbers?

Yeah. As-is, downto wouldn't work with unsigned numbers. That would require some new codegen—which, however, we could always add later.

brianrourkeboll avatar Feb 11 '25 22:02 brianrourkeboll

What will the user experience when using downto to unsigned integral types? Is the message and range for it after this PR good enough?

(existing integral for .. to .. loop just emits the generic FS0001, so the bar is low here)

T-Gro avatar Feb 12 '25 08:02 T-Gro

What will the user experience when using downto to unsigned integral types? Is the message and range for it after this PR good enough?

(existing integral for .. to .. loop just emits the generic FS0001, so the bar is low here)

It will tell the user that the type does not support the unary minus operator ~-, i.e., the same error they would get if they had written start .. -1u𝑥 .. finish themselves.

That is probably not ideal, since the user wrote start downto finish and not start .. -1u𝑥 .. finish.

If we wanted to emit an error more like "downto is not supported for unsigned numeric types," we'd need to do something like keep track of the fact that the AST originally had downto through to the point in time when we typecheck the synthetic range expression with the negative step and it fails.

brianrourkeboll avatar Feb 12 '25 15:02 brianrourkeboll

It will tell the user that the type does not support the unary minus operator ~-, i.e., the same error they would get if they had written start .. -1u𝑥 .. finish themselves.

That is probably not ideal, since the user wrote start downto finish and not start .. -1u𝑥 .. finish.

Hmm, it's not really that bad, though:

> for n = 10uy downto 1uy do printfn $"{n}";;

  for n = 10uy downto 1uy do printfn $"{n}";;
  -------------^^^^^^

stdin(1,14): error FS0043: The type 'byte' does not support the operator '~-'

brianrourkeboll avatar Feb 16 '25 19:02 brianrourkeboll