# Defining method for type before declaring that type

**URL:** <https://discourse.julialang.org/t/defining-method-for-type-before-declaring-that-type/38164>\
**Category:** New to Julia\
**Created:** [April 24, 2020, 7:53pm UTC](https://discourse.julialang.org/t/defining-method-for-type-before-declaring-that-type/38164 "2020-04-24T19:53:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cojua8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cojua8/32/11882_2.png) [@cojua8](https://discourse.julialang.org/u/cojua8)\
**Post date:** [April 24, 2020, 7:53pm UTC](https://discourse.julialang.org/t/defining-method-for-type-before-declaring-that-type/38164/1 "2020-04-24T19:53:40Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 24, 2020, 7:56pm UTC](https://discourse.julialang.org/t/defining-method-for-type-before-declaring-that-type/38164/2 "2020-04-24T19:56:54Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [April 24, 2020, 8:47pm UTC](https://discourse.julialang.org/t/defining-method-for-type-before-declaring-that-type/38164/3 "2020-04-24T20:47:56Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 24, 2020, 8:50pm UTC](https://discourse.julialang.org/t/defining-method-for-type-before-declaring-that-type/38164/4 "2020-04-24T20:50:43Z")

</div>

OP using Abstract types is the correct answer.
