You can just define

function myf(x)
    # do things with x, assuming it is of type T1 or T2
end


This is called "duck typing <https://en.wikipedia.org/wiki/Duck_typing>".
This way the function will work with and x that supports the operations
that myf applies to it. If you would prefer for the method definition to
only apply to x when it's of type T1 or T2 you can use a union:

function myf(x::Union{T1,T2})

    # x will always be of either type T1 or T2

end


On Thu, Dec 17, 2015 at 5:42 AM, <[email protected]> wrote:

> Hi,
>
> I'm writing a module inside which I want to define a function for two
> different types T1 and T2 that are defined outside the module, and there is
> no relation available (they are not subtypes of another type...).
> How could I write only one function instead of having code duplication?
>
> Currently, my code is as follows:
>
> function myf{T1}(x::T1)
> ...
> end
>

Reply via email to