Skip to content

How to get global variable in resumed function. #130

Closed Answered by Krastanov
wisdom1972 asked this question in Q&A
Discussion options

You must be logged in to vote

Here is a quick generic example (basically you can use a global like in a usual function)

julia> using ResumableFunctions

julia> const mygloblist = [];

julia> @resumable function f()
           while true
               a = length(mygloblist)
               push!(mygloblist, a)
               @yield a
               a > 10 && break
           end
       end
f (generic function with 1 method)

julia> collect(f())
12-element Vector{Any}:
  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11

julia> mygloblist
12-element Vector{Any}:
  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11

I will marked this as resolved, but do not hesitate to reopen it if you have additional quesitons

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Krastanov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #129 on September 15, 2024 22:47.