I am trying to implement multiple dispatch to give users a default option but also rise an error if the type chosen doesn’t implement the specific method.
It works fine if the method implement positional argument, but not if these are keywork argument.
Why is that? Any workaround ?
# My type structure :
abstract type MyAbstractType end
mutable struct Concrete1 <: MyAbstractType
a
b
function Concrete1(;a=1,b=2)
new(a,b)
end
end
mutable struct Concrete2 <: MyAbstractType
c
d
function Concrete2(;c=10,d=20)
new(c,d)
end
end
mutable struct Concrete3 <: MyAbstractType
e
f
function Concrete3(;e=100,f=200)
new(e,f)
end
end
# Positional arguments: works as expected
function f2(x,obj::MyAbstractType)
println("Not implemented")
end
function f2(x,obj::Concrete1=Concrete1())
println(obj.a)
end
function f2(x,obj::Concrete2)
println(obj.c)
end
f2(1) # 1
f2(1,Concrete1()) # 1
f2(1,Concrete1(a=2,b=3)) # 2
f2(1,Concrete2()) # 10
f2(1,Concrete2(c=20,d=30)) # 20
f2(1,Concrete3()) # "Not implemented"
# Keyword arguments:generate an error on f(1) ("keywork argument obj not assigned")
function f(x;obj::MyAbstractType)
println("Not implemented")
end
function f(x;obj::Concrete1=Concrete1())
println(obj.a)
end
function f(x;obj::Concrete2)
println(obj.c)
end
f(1) # UndefKeywordError: keyword argument obj not assigned
f(1,obj=Concrete1())
f(1,obj=Concrete1(a=2,b=3))
f(1,obj=Concrete2())
f(1,obj=Concrete2(c=20,d=30))
f(1,obj=Concrete3())