Method overwritten by keyword-arguments-only version

I’d like to have two versions of a function, with and without kwargs. On Julia 0.7, these two lines run without problem:

f(x=3) = x+3
f(; x=3) = f(x)

however, upon compilation of the module, it says

WARNING: Method definition f() in module ...at ...\...:27 overwritten at ...\...:28.

Is it a bad thing to do?

It is a true overwrite. In this case it’s not “bad” since the two are actually doing the same thing. However, to reduce code duplication you cn just not have that optional positional argument and let the kw version provide the f().

You mean keeping only the keyword version? Yeah but then if I have let’s say only g(; x=3) = x + 3 I can’t use it without keywords (g(5)) :slightly_frowning_face: I’d like to have both worlds, but I guess I have to chose.

No. I mean not having the optional positional argument, not removing that whole function. Just remove that =3 from the f(x=3). That also,

1 Like
f(_x=3; x=_x) = x+3
f(3) # => 6
f(x=3) # => 6
2 Likes

Interesting hack!

Thanks for your answers