Stream: helpdesk (published)

Topic: Specializing keyword arguments based on type


view this post on Zulip Lucien Huber (Aug 23 2022 at 10:43):

I have a function that has some default keyword arguments, and operates on an abstract type that I have defined e.g

f(a::A;kw=default)=#stuff

I would now like to specify this method with a type B<:A , without actually affecting the function itself just changing the default kwarg:

f(b::B;kw=anotherDefault)=#same stuff

Ideally I don't need to copy the body of the function..

view this post on Zulip Sebastian Pfitzner (Aug 23 2022 at 10:53):

you could either do something like

f(a::T; kw = T === A ? default : anotherDefault) where T <: Union{A,B}

or put the actual implementation into a different method

f(a::A;kw=default) = _f(a, kw)
f(a::B;kw=anotherDefault) = _f(a, kw)
_f(a, kw) = ...

Last updated: Nov 22 2024 at 04:41 UTC