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
Multiple dispatch is great, but it sometimes is the wrong tool for the job. In this case, basic dispatch on function names is simple and sufficient. I would prefer this option if at all possible. However, if a consistent function name is necessary, then you can always dispatch on custom types.
function save_image_url(url::String; kwargs...)
handle_general_options(; kwargs...)
# url specific code
end
function save_image_path(path::String; kwargs...)
handle_general_options(; kwargs...)
# path specific code
end
function handle_general_options(; title=nothing, description=nothing)
# shared code
end
struct ImageURL
val::String
end
function save(url::ImageURL; kwargs...)
handle_metadata(; kwargs...)
# url specific code
end
function handle_general_options(; title=nothing, description=nothing)
# shared code
end
You can make dispatch clear at the call site by immediately creating the object to dispatch on:
save(ImageURL("https://..."); title="SomeTitle")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With