Optional args and the methods they imply


#1
# https://docs.julialang.org/en/stable/manual/methods/#man-note-on-optional-and-keyword-arguments
# Optional args are implemented as syntax for multiple method definitions, e.g.
f(a=1,b=2) = a+2b 

# which translates into three methods
f(a,b) = a+2b # explicit/optional a,b args
f(a) = f(a,2) # explicit/optional a arg
f() = f(1,2) # no optional args, f() call equivalent to f(1,2) call, which invokes f(a,b) and results to 5 

I’m assuming method f(b) = f(1,2b) is implicit here? How do I call it? E.g. f(,8) didn’t work.
@yuyichao @Michael_Eastwood


#2

No.

You can’t


#3
f(a=1,b=5,c=9,d=10) = a+3b+9c+8d

f(b,c) = f(1,3b,9c,10) # not possible either?

#4

No. You are also writing the juxtapose syntax. The name of the position arguments has no significant at all. Use learn to do what you want


#5

Or I should say it’s valid syntax, which you should just test yourself, but it’s unclear what you want to do other than overwriting a method


#6

Oh I wasn’t planning on overwriting anything, just trying to understand how optional args and their methods work. So optional args can only be called all at once, none at all, or from the first positional argument a. You can’t call more than one optional arg from say a tuple of five args. That’s my understanding and I don’t know the criteria for that decision.


#7

Presumably, it’s just a simple enough implementation of that concept. If you have more than 1 or 2 optional arguments, you should probably be using keyword arguments anyway.


#8

Wrong. You can call it with any number of arguments. Missing arguments will use the default value.


#9

Maybe an example would be helpful?

julia> f(a="default", b="default", c="default", d="default") = @show a b c d
f (generic function with 5 methods)

julia> f("not the default", "not the default", "not the default", "not the default");
a = "not the default"
b = "not the default"
c = "not the default"
d = "not the default"

julia> f("not the default", "not the default", "not the default");
a = "not the default"
b = "not the default"
c = "not the default"
d = "default"

julia> f("not the default", "not the default");
a = "not the default"
b = "not the default"
c = "default"
d = "default"

julia> f("not the default");
a = "not the default"
b = "default"
c = "default"
d = "default"

julia> f();
a = "default"
b = "default"
c = "default"
d = "default"

#10

This is what I wanted to know

julia> f(a="default",d="default"; b="default",c="default") = @show a b c d
f (generic function with 3 methods) # b and c are keyword args

julia> f(b="not default", c="not default");
a = "default"
b = "not default"
c = "not default"
d = "default"

Thanks!


#11

You can only do that using keyword arguments. f uses only positional arguments and when using positional arguments, you can only give as input to the function the first k arguments with the last n-k arguments being the default values.


#12

That’s even better, thanks! Then there’s no point in using keyword args. Just place all optionals at the beginning or end of list

julia> f( b="default",a="default", d="default",c="default") = @show a b c d
f (generic function with 5 methods)

julia> f(b="not default", c="not default");
a = "default"
b = "not default"
c = "not default"
d = "default"

#13

Try restart Julia and do that again.


#14

Please read and stick to the doc. kw args and positional args are totally different. You don’t need to find a syntax to make them the same.


#15

Thanks Kris and Yichao, I’ll stick to keyword args to do what they were meant for.