# Static methods

**URL:** https://discourse.julialang.org/t/static-methods/68945
**Category:** New to Julia
**Created:** [September 29, 2021, 4:43pm UTC](https://discourse.julialang.org/t/static-methods/68945 "2021-09-29T16:43:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![whatsthecraic](https://avatars.discourse-cdn.com/v4/letter/w/7bcc69/32.png) [@whatsthecraic](https://discourse.julialang.org/u/whatsthecraic)
#### Post date: [September 29, 2021, 4:43pm UTC](https://discourse.julialang.org/t/static-methods/68945/1 "2021-09-29T16:43:47Z")

</div>

Imagine there are a bunch of `static methods` (a la C++/Java) defined like:

```julia
foo(::Any) = 0
foo(::A) = 1
foo(::T) where (T <: B} = 2
foo(::C) = 3

function trampoline(t) 
 ... large code chunk here that doesn't depend on t...
 foo(t)
 ... more code here that doesn't depend on t...
end

```

I think this is called the parametric type pattern in [the book by Tom Kwong](https://www.packtpub.com/product/hands-on-design-patterns-and-best-practices-with-julia/9781838648817).  
I would like to avoid specializing & recompiling trampoline(t) for every invoked parameter type. Is there any manner to avoid that, assuming that foo(x) is a static function (it doesn’t depend on any instance field).

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [September 29, 2021, 4:58pm UTC](https://discourse.julialang.org/t/static-methods/68945/2 "2021-09-29T16:58:40Z")

</div>

> [@whatsthecraic](#):
>
> I would like to avoid specializing & recompiling trampoline(t) for every invoked parameter type.

See the `@nospecialize` macro.

(For the vast majority of functions, this is not beneficial. In normall well written Julia code, you shouldn’t find yourself recompiling functions over and over or doing lots of dynamic dispatch.)

---

<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: [September 29, 2021, 4:59pm UTC](https://discourse.julialang.org/t/static-methods/68945/3 "2021-09-29T16:59:52Z")

</div>

I would use

```julia
function chunk1(#=args excluding t=#)
    # ... large code chunk here that doesn't depend on t...
end
function chunk2(#=args excluding t=#)
    # ... large code chunk here that doesn't depend on t...
end

function trampoline(t, args...)
    chunk1(args...)
    foo(t)
    chunk2(args...)
end

```
