ConferenceCall.jl

Author cstjean
Popularity
5 Stars
Updated Last
2 Years Ago
Started In
February 2020

ConferenceCall

ConferenceCall.jl allows multiple methods to be defined with the "same" signature (some Val{unique_key} is added behind the scene to make them different). Calls to that function will call all applicable methods, and return their results in a Vector.

@confcall function ask_for_advice end
@confcall ask_for_advice() = "Buy!"
@confcall ask_for_advice() = "Sell!"
@confcall ask_for_advice(whom::String) = "I didn't say anything"

julia> ask_for_advice()
2-element Array{String,1}:
 "Sell!"
 "Buy!" 

julia> ask_for_advice("Harry")
1-element Array{String,1}:
 "I didn't say anything"

julia> ask_for_advice(:BobSymbol)
0-element Array{Union{},1}

The methods are called in sorted order, based on the optional key passed as first argument:

@confcall function describe_object end
@confcall 1 describe_object(x) = "Something"
@confcall 2 describe_object(x::Number) = "Some number"
@confcall 3 describe_object(x::Int) = "An Int"

julia> describe_object(3.0)
2-element Array{String,1}:
 "Something"  
 "Some number"

Keys can be Numbers or Symbols.

Because it is based on plain Julia methods, @confcall is precompilation-friendly and Reviseable. It can be useful for macros that register callbacks.

Making a conference call is moderately slow, as it involves some reflection.

julia> @btime describe_object(3.0)
  32.748 μs (38 allocations: 1.69 KiB)
2-element Array{String,1}:
 "Something"  
 "Some number"

The reflection can be done at compile-time using @confcall_fast:

julia> @confcall_fast function describe_object end
describe_object (generic function with 1 method)

julia> @btime describe_object(3.0)
  7.377 ns (1 allocation: 32 bytes)
("Something", "Some number")

but it involves generated functions, so it revises poorly (as of Julia 1.4). Adding/removing methods will not work after the first call.