dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.82k stars 773 forks source link

Support TailCallAttribute in nested functions #17274

Closed esbenbjerre closed 3 weeks ago

esbenbjerre commented 3 weeks ago

I often find myself embedding tail recursive functions inside other functions. Since F# 8.0 we can use the [<TailCall>] attribute, but it doesn't compile for nested functions. As an example I would like for the code below to compile.

let fib n
  =
  [<TailCall>]
  let rec loop i a b =
    if i = n then a
    else loop (i + 1) b (a + b)
  loop 0 0 1

but instead I get

error FS0010: Unexpected symbol '[<' in binding
vzarytovskii commented 3 weeks ago

That should be a language suggestion to support attributes on bindings inside bindings, since it changes allowed expressions. Please, reopen there.