-
Notifications
You must be signed in to change notification settings - Fork 7
Open
Description
I think we can make better use of dispatch if we employ the Singleton pattern as described in
https://www.packtpub.com/en-us/product/hands-on-design-patterns-and-best-practices-with-julia-9781838646615
This is practically identical to Val{T} but can be used with a custom more obviously named type as well.
This would greatly simplify some of the functions in dispatch.jl, e.g. plottertype
This is an example of how it could look like
abstract type AbstractPlotterType end
struct PlotterType{T} <: AbstractPlotterType end #The singleton type
plottertype(m::Union{Module,Nothing}) = PlotterType{nameof(m)}() #Return singleton
plottername(::Plottertype{T}) where {T} = String(T)
const PythonPlotType = PlotterType{:PythonPlot} #Shorthand for compatibility
const PyPlotType = PlotterType{:PyPlot}
const AbstractPythonPlotterType = Union{PyPlotType,PythonPlotType} #makes PyPlotType <: AbstractPythonPlotterType true
We can verify things work as expected (in the small test)
PyPlotType === PlotterType{:PyPlot} # returns true
PyPlotType() === PlotterType{:PyPlot}() # returns true
p1 = PyPlotType()
p2 = PyPlotType()
p1 === p2 # returns true since PyPlotType is a singleton
function foo(a::T) where {T<:AbstractPythonPlotterType}
@info "Yay"
end
foo(PyPlotType()) # Outputs [ Info: Yay
So I think this would pretty much be a drop-in replacement but before I go through all of dispatch.jl and prepare a PR I wanted to suggest it first to see what you think.
Metadata
Metadata
Assignees
Labels
No labels