-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
do-block with keyword arguments #50395
Comments
Related: #40313 |
I think it would need to be just_call(1; z = 2) do (x; z)
x + z
end to avoid ambiguity with the |
@simonbyrne makes a good point against this here https://discourse.julialang.org/t/allowing-for-keyword-arguments-in-do-block/19244 That syntax wouldn’t really be consistent, since f(args) do (x,y)
#body
end gives an anonymous function which takes a single tuple as an argument, i.e. is f(function ((x,y),)
#body
end, args) The way to specify a multiple argument function is without parentheses, i.e. f(args) do x,y
#body
end Unfortunately using a semicolon as a keyword separator won’t work, as it is already used as an expression delimiter, i.e. f(args) do x,y; kw=1
#body
end is equivalent to f(function (x,y)
kw = 1
#body
end, args) Another solution : |
This is highly related to #54915 - the root cause is the same and the "real solution" IMO is to do what Jeff suggested here #54915 (comment) See #54915 (comment) and #54903 for ways we might achieve this breaking change without waiting for Julia 2.0 |
Thanks for your quick feedback. |
Ok, I may find a solution for This issue. A=[1,2,3]
B=[4,5,6]
C=[(),(:info => false,),(:info => true, :msg => "tyf")]
function func(a,b;info=true,msg="CSC")
if info
@info "Hi, $msg"
@info "calling func with a=$a , b=$b , info=$info and msg=$msg"
end
@info "__________________"
a+b
end
hh=map(A,B,C) do args...
func( args[begin : end-1]...; args[end]...)
end this will produce output as this [ Info: Hi, CSC
[ Info: calling func with a=1 , b=4 , info=true and msg=CSC
[ Info: __________________
[ Info: __________________
[ Info: Hi, tyf
[ Info: calling func with a=3 , b=6 , info=true and msg=tyf
[ Info: __________________
3-element Vector{Int64}:
5
7
9
As you can see, this helps send needed keyword arguments via Do-Block Args easily without modifying the native Julia code base. Happy Coding |
If that can help # def
f(g, args...; kwargs...) = g((; kwargs...), args...)
|
I think it should be f(g, args...; kwargs...) = g(kwargs, args...) f(1,2,3; a=1, b=2) do kwargs, args...
(args..., (kwargs...,))
end generate output as
which can be used in Do Block.. Could you correct me if I'm wrong/miss something?? Happy Coding |
then we can modify and improve it A=[1,2,3]
B=[4,5,6]
C=[(),(:info => false,),(:info => true, :msg => "tyf")]
in this case, function func not need to modify function func(a,b;info=true,msg="CSC")
if info
@info "Hi, $msg"
@info "calling func with a=$a , b=$b , info=$info and msg=$msg"
end
@info "__________________"
a+b
end hh=map(C, A, B) do kwds,args...
func( args...; kwds...)
end which generated the same output but did not need to use Range Unit.. [ Info: Hi, CSC
[ Info: calling func with a=1 , b=4 , info=true and msg=CSC
[ Info: __________________
[ Info: __________________
[ Info: Hi, tyf
[ Info: calling func with a=3 , b=6 , info=true and msg=tyf
[ Info: __________________
3-element Vector{Int64}:
5
7
9 julia> hh
3-element Vector{Int64}:
5
7
9 hope this can help!! Happy Coding |
Somewhat a matter of choice.
About the syntax. form using Test
nt = (a=1, b=2)
@test nt == (a = 1, b = 2)
@test (nt..., c=3) == (a = 1, b = 2, c = 3)
@test (nt..., ) == (1, 2)
nt = (; a=1, b=2)
@test nt == (; a=1, b=2)
@test (; nt..., c=3) == (; a=1, b=2, c=3)
@test (; nt...) == (; a=1, b=2) |
yes, it's a matter of choice. C=[(),(info = false,),(info = true, msg = "tyf")]
the result will still be the same. somehow NT is more convenient for accessing data compared to Pairs, but I think Kwds is more like a Pairs rather than NT in nature. maybe it is related to the speed or performance of Julia in runtime. as Julia is New compared to other programming languages, Polluted and hard to read are common, Happy Coding |
Quite agree with your points. |
Yes, wise choice among others... Happy Coding |
@cheengshuchin: It is understood that you can work around this issue with a closure (and a macro, etc), that is not the point of opening an issue. Please continue unrelated discussion on Discourse. |
So this is a discussion thread for feature requests by throwing an issue thicket in here?! as I just trying to help by offering another way to solve it via encapsulating Keyword Arguments in normal positional arguments in normal Do-Block without using any fancy technique like closure or macro etc (Which I using in Decorator/Convolution via Julia Do-Block Syntac). Sorry as I did not deliver what you expected for. Happy Coding and Nice Discussion |
I could not find out how to use do-blocks with keyword arguments, so either it is not possible or underdocumented. Cf
and the "logical" do-block version would be
which fails with
(FWIW, this is on Julia 1.9.1).
Cf discourse, I could not find an open issue.
The text was updated successfully, but these errors were encountered: