ResumableFunctions.jl icon indicating copy to clipboard operation
ResumableFunctions.jl copied to clipboard

Syntax error when using `@NamedTuple` types inside resumable function

Open lgoettgens opened this issue 1 year ago • 4 comments

Describe the problem

One cannot use the @NamedTuple syntax (introduced in julia 1.5) inside a @resumable function, if one of the keys of the named tuple is also present as a local variable of that function. I expect the @NamedTuple{foo::Int64} syntax in the mwe below to be equivalent to replacing it by Base.NamedTuple{(:foo,), Base.Tuple{Int64}}. However, the latter works as expected, but the former throws ERROR: syntax: invalid named tuple field name "_fsmi.foo" around REPL[26]:2.

Minimal Working Example

@resumable function bar()
   vec = (foo = 3,)::@NamedTuple{foo::Int64}
  foo = "unused"
  @yield vec
end
@resumable function bar2()
  vec = @NamedTuple{foo::Int64}((foo = 3,))
  foo = "unused"
  @yield vec
end

Package versions

Please provide the versions you use. To do this, run the code:

julia> Pkg.status(["ResumableFunctions"]; mode = PKGMODE_MANIFEST)
Status `/tmp/jl_MokoyF/Manifest.toml`
  [c5292f4c] ResumableFunctions v0.6.10

lgoettgens avatar Sep 13 '24 12:09 lgoettgens

Can you try with #100?

thofma avatar Sep 13 '24 12:09 thofma

Works like a charm. Another issue you can add to the "closing" list in your top comment :) (btw you need to repeat the word "closing" for each referenced issue for github to track it correctly)

lgoettgens avatar Sep 13 '24 12:09 lgoettgens

Correcting my previous message: The mwe from the initial message works with #100. But if one reorders the lines (the local var foo gets now created before using foo as a key in @NamedTuple), there is an error when trying to execute the function (see below).

julia> @resumable function bar()
  foo = "unused"
  vec = (foo = 3,)::@NamedTuple{foo::Int64}
  @yield vec
end

julia> @resumable function bar2()
  foo = "unused"
  vec = @NamedTuple{foo::Int64}((foo = 3,))
  @yield vec
end

julia> collect(bar())
ERROR: TypeError: in Type, in parameter, expected Type, got a value of type Tuple{Expr}
Stacktrace:
 [1] ##bar_FSMI#230
   @ ~/.julia/packages/ResumableFunctions/IOa5O/src/macro.jl:3 [inlined]
 [2] ##bar_FSMI#230
   @ ~/.julia/packages/ResumableFunctions/IOa5O/src/macro.jl:166 [inlined]
 [3] iterate (repeats 2 times)
   @ ~/.julia/packages/ResumableFunctions/IOa5O/src/types.jl:24 [inlined]
 [4] _collect(cont::UnitRange{Int64}, itr::var"##bar_FSMI#230"{Union{}, String}, ::Base.HasEltype, isz::Base.SizeUnknown)
   @ Base ./array.jl:770
 [5] collect(itr::var"##bar_FSMI#230"{Union{}, String})
   @ Base ./array.jl:759
 [6] top-level scope
   @ REPL[8]:1

lgoettgens avatar Sep 13 '24 12:09 lgoettgens

It is basically the same as #54. I have some local branch for better macro support, but it requires #100.

thofma avatar Sep 13 '24 12:09 thofma