Library
Public interface
ResumableFunctions.ResumableFunctions
— ModuleMain module for ResumableFunctions.jl – C# style generators a.k.a. semi-coroutines for Julia
ResumableFunctions.@nosave
— MacroMacro if used in a @resumable function
that creates a not saved variable otherwise throws an error.
ResumableFunctions.@resumable
— MacroMacro that transforms a function definition in a finite-state machine:
- Defines a new
mutable struct
that implements the iterator interface and is used to store the internal state. - Makes this new type callable having following characteristics:
- implementents the statements from the initial function definition but;
- returns at a
@yield
statement and; - continues after the
@yield
statement when called again.
- Defines a constructor function that respects the calling conventions of the initial function definition and returns an object of the new type.
If the element type and length is known, the resulting iterator can be made more efficient as follows:
- Use
length=ex
to specify the length (if known) of the iterator, like: @resumable length=ex function f(x); body; end Hereex
can be any expression containing the arguments off
. - Use
function f(x)::T
to specify the element type of the iterator.
Extended
julia> @resumable length=n^2 function f(n)::Int
for i in 1:n^2
@yield i
end
end
f (generic function with 2 methods)
julia> collect(f(3))
9-element Vector{Int64}:
1
2
3
4
5
6
7
8
9
ResumableFunctions.@yield
— MacroMacro if used in a @resumable function
that returns the expr
otherwise throws an error.
ResumableFunctions.@yieldfrom
— MacroMacro if used in a @resumable function
that delegates to expr
otherwise throws an error.
Internals
Base.IteratorSize
— MethodImplements the iteratorsize
method of the iterator interface for a subtype of FiniteStateMachineIterator
.
ResumableFunctions.BoxedUInt8
— TypeMutable struct that contains a single UInt8
.
ResumableFunctions.FiniteStateMachineIterator
— TypeAbstract type used as base type for the type created by the @resumable
macro.
Base.eltype
— MethodImplements the eltype
method of the iterator interface for a subtype of FiniteStateMachineIterator
.
ResumableFunctions._is_yield
— MethodFunction returning whether an expression is a @yield
macro
ResumableFunctions._is_yieldfrom
— MethodFunction returning whether an expression is a @yieldfrom
macro
ResumableFunctions.forward_args
— MethodTakes a function definition and returns the expressions needed to forward the arguments to an inner function. For example function foo(a, ::Int, c...; x, y=1, z...)
will
- modify the function to
gensym()
nameless arguments - return
(:a, gensym(), :(c...)), (:x, :y, :(z...)))
ResumableFunctions.get_args
— MethodFunction returning the arguments of a function definition
ResumableFunctions.get_param_name
— MethodFunction returning the name of a where parameter
ResumableFunctions.get_slots
— MethodFunction returning the slots of a function definition
ResumableFunctions.remove_catch_exc
— MethodFunction removing the exc
symbol of a catch exc
statement of a list of slots.
ResumableFunctions.transform_arg
— MethodFunction that replaces a arg = @yield ret
statement by
@yield ret;
arg = arg_
where arg_
is the argument of the function containing the expression.
ResumableFunctions.transform_arg_yieldfrom
— MethodFunction that replaces an arg = @yieldfrom iter
statement by
@yieldfrom iter
arg = _ret_.value
ResumableFunctions.transform_continue
— MethodFunction that replaces a continue
statement by a corresponding @goto
with as label the correct location for the next iteration.
ResumableFunctions.transform_exc
— MethodFunction that replaces a @yield ret
or @yield
statement by
@yield ret
_arg isa Exception && throw(_arg)
to allow that an Exception
can be thrown into a @resumable function
.
ResumableFunctions.transform_for
— MethodFunction that replaces a for
loop by a corresponding while
loop saving explicitly the iterator and its state.
For loops of the form for a, b, c; body; end
are denested.
ResumableFunctions.transform_nosave
— MethodFunction that replaces a variable
ResumableFunctions.transform_remove_local
— MethodFunction that removes local x
expression.
ResumableFunctions.transform_slots
— MethodFunction that replaces a variable x
in an expression by _fsmi.x
where x
is a known slot.
ResumableFunctions.transform_try
— MethodFunction that replaces a try
-catch
-finally
-end
expression having a top level @yield
statement in the try
part
try
before_statements...
@yield ret
after_statements...
catch exc
catch_statements...
finally
finally_statements...
end
with a sequence of try
-catch
-end
expressions:
try
before_statements...
catch
catch_statements...
@goto _TRY_n
end
@yield ret
try
after_statements...
catch
catch_statements...
end
@label _TRY_n
finally_statements...
ResumableFunctions.transform_yield
— MethodFunction that replaces a @yield ret
or @yield
statement with
_fsmi._state = n
return ret
@label _STATE_n
_fsmi._state = 0xff
ResumableFunctions.transform_yield
— MethodFunction that replaces a @yield ret
or @yield
statement with
Base.inferencebarrier(ret)
This version is used for inference only. It makes sure that val = @yield ret
is inferred as Any
rather than typeof(ret)
.
ResumableFunctions.transform_yieldfrom
— MethodFunction that replaces a @yieldfrom iter
statement with
_other_ = iter...
_ret_ = generate(_other_, nothing)
while !(_ret_ isa IteratorReturn)
_value_, _state_ = _ret_
_newvalue_ = @yield _value_
_ret_ = generate(_other_, _newvalue_, _state_)
end
_