Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia multiple dispatch based on named arguments?

Tags:

julia

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
like image 482
Alex Craft Avatar asked Dec 11 '22 01:12

Alex Craft


1 Answers

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.

Different Function Names

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

Custom Types

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")
like image 180
David Varela Avatar answered Dec 25 '22 02:12

David Varela