Defining method for type before declaring that type

Hi,

For some reason (I know that it can be bad architecture), I need to write functions for a type that is not yet declared.

Is there a way to do this?

Nope. You can’t write f(x::MyType) = ... before MyType is defined.

You can, however, write f(x) = ... at any time and then pass in a MyType to f after you’ve defined MyType.

1 Like

You can use abstract types to avoid needing to define fully general methods.

julia> abstract type AbstractFoo end

julia> bar(::AbstractFoo) = 3
bar (generic function with 1 method)

julia> struct Foo{T} <: AbstractFoo
           x::T
       end

julia> bar(Foo(2))
3
7 Likes

OP using Abstract types is the correct answer.