-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: drop/ignore return value rather than yielding it #15
Comments
Also related to #2. |
Hi I am aware of this "feature";) I use A more important problem is how your proposal can be implemented. When a value is yielded (implemented as a normal return) the In Julia a function returns always something (nothing) so I can not create a So I am a little bit stuck, I understand your motivations for this modification but I see no way how to implement it. If you have any ideas how to do this. Feel free to share it and I am willing to integrate it in the package. Kind regards Ben |
Hi Ben, Thank you for the reply. I have only scratched the surface so far, but let me mention a few things that come to mind. I think Python is able to deal with this issue in the way I expect because its iteration protocol relies on the |
just to say I also just tried this package and probably have to give up on it because of this. Not being able to ignore the final return makes it much much more tedious to use. (otherwise I love it!) unfortunately I don't have an idea either how to do this. |
Please check out branch Python-generators. Can you confirm that this is the behaviour you really like? Ben |
Only on Julia v0.7 this is supported... I will back port if asked for |
I did a quick back-port to Julia v0.6, so check out branch Python-generators-v0.6 |
Thank you, I’ll try to test this tonight. |
Thank you! One difference between this and python's generators is that the values are always calculated one ahead of what is currently necessary. For instance, @resumable function f(n)
for i in 1:n
println("yielding $i")
@yield i
end
end
for a in f(4)
println(a)
end yields
This may be a reasonable stopgap measure, though, until JuliaLang/julia#18823 is addressed. I also noticed one issue when specifying the type of the return value, which I believe is a surmountable implemention issue: @resumable function f(n)::Int
for i in 1:n
@yield i
end
end
collect(f(4))
ERROR: MethodError: Cannot `convert` an object of type Nothing to an object of type Int64 |
Hi I found inspiration in the new Julia iteration protocol that behaves identically: values are always calculated one ahead. When specifying the return type. The function has to return the correct type... because values are calculated one ahead. A simple solution can be: @resumable function f(n)::Int
for i in 1:n
@yield i
end
0
end
collect(f(4)) I do some further testing and if no other problems are raised I will release somewhere next week an updated version. |
After checking out the new branch: (on v0.6) julia> using ResumableFunctions
INFO: Precompiling module ResumableFunctions.
ERROR: LoadError: LoadError: UndefVarError: KeySet not defined
Stacktrace:
[1] include_from_node1(::String) at ./loading.jl:576
[2] include(::String) at ./sysimg.jl:14
[3] include_from_node1(::String) at ./loading.jl:576
[4] include(::String) at ./sysimg.jl:14
[5] anonymous at ./<missing>:2
while loading /Users/ortner/.julia/v0.6/ResumableFunctions/src/transforms.jl, in expression starting on line 521
while loading /Users/ortner/.julia/v0.6/ResumableFunctions/src/ResumableFunctions.jl, in expression starting on line 14
ERROR: Failed to precompile ResumableFunctions to /Users/ortner/.julia/lib/v0.6/ResumableFunctions.ji.
Stacktrace:
[1] compilecache(::String) at ./loading.jl:710
[2] _require(::Symbol) at ./loading.jl:497
[3] require(::Symbol) at ./loading.jl:405 |
Checkout branch Python-generators-v0.6 and it will work on Julia v0.6 |
fantastic - thanks. The other problem I had last time I tried your package was performance related. But I wasn't sure whether it was my fault or a limitation of this package. If I can produce a MWE then I'll just file another issue. |
I don't think this is correct. The new iteration protocol always calls
under the new protocol. |
You're right. Now it should do things in the correct order (Julia v0.6) and also the issue when the return type is specified, is solved. Now it should do the same as the revised iteration protocol. Ben |
Wow that's awesome! :-) Thank you. Here is a related idea/suggestion: perhaps the macro should error if there is a |
I use the |
Is there an example of this in action somewhere? Is the intended effect any different from |
(Actually, I can't find a single example of a file that contains both |
There was (is) a use case but as you have mentioned we can always do |
Sounds good! Another possibility would be to have a deprecation warning instead of an error. |
Bump |
I have implemented your request in branch Python-generators-v0.6 If I get no negative feedback, I will make it an error. This will be the default behaviour for Julia v0.7. Ben |
As far as I can tell, this now behaves precisely how I would like it to. One (minor) remaining thing:
I would suggest changing this line to
This way, a Kind regards, |
Hi there
I'm very excited about ResumableFunctions.jl. It is a wonderful demonstration of the power of julia's macro system.
On the other hand, I find one design choice to be puzzling: the choice to yield the final/return value of a resumable function as an additional value. I propose that instead, all such values should be
@yield
ed explicitly, and any such return value should be dropped.This would have a number of advantages:
See also issue #13, which may be related.
The text was updated successfully, but these errors were encountered: