I'm trying to create flexible function that would allow variety of uses:
save(image_url = "http://...")
save(image_url = "http://...", title = "Cats", id = "cats")
save(text = "Some text", comments = "Some comments")
save(text = "Another text", title = "News", compression = true)
Basically it's 3 (or more) functions (save_image_url
, save_image_path
, save_text
) combined .
All of them have 3 same optional arguments title
, descriptions
, compression
. And each can have any number of specific arguments.
I don't want to use positional arguments because it would be hard to remember the order of arguments. Also, the first argument would have the same String
type for image_url
and image_path
and text
.
Unfortunately it seems multiple dispatch is not working on named arguments. What are the patterns to handle such cases then?
Not working implementation
function save(;
image_url:: String,
title:: Union{String, Nothing} = nothing,
description:: Union{String, Nothing} = nothing,
compression:: Union{Bool, Nothing} = nothing
)::Nothing
nothing
end
function save(;
image_path:: String,
title:: Union{String, Nothing} = nothing,
description:: Union{String, Nothing} = nothing,
compression:: Union{Bool, Nothing} = nothing
)::Nothing
nothing
end
function save(;
text:: String,
comments:: Union{String, Nothing} = nothing,
title:: Union{String, Nothing} = nothing,
description:: Union{String, Nothing} = nothing,
compression:: Union{Bool, Nothing} = nothing
)::Nothing
nothing
end
f(;a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8)
would have9! = 362880
different methods. Yikes! – Mason