# Override foo{x}

**URL:** https://discourse.julialang.org/t/override-foo-x/61969
**Category:** General Usage
**Created:** [May 28, 2021, 3:47am UTC](https://discourse.julialang.org/t/override-foo-x/61969 "2021-05-28T03:47:35Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [May 28, 2021, 3:47am UTC](https://discourse.julialang.org/t/override-foo-x/61969/1 "2021-05-28T03:47:35Z")

</div>

Is it possible to customize what happens when I do `foo{x}` for a particular type `Foo` or type `X`?

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [May 28, 2021, 5:11am UTC](https://discourse.julialang.org/t/override-foo-x/61969/2 "2021-05-28T05:11:08Z")

</div>

Nope. You could write a macro though, e.g. `@bar foo{x}` can mean whatever you want it to mean.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [May 28, 2021, 5:20am UTC](https://discourse.julialang.org/t/override-foo-x/61969/3 "2021-05-28T05:20:16Z")

</div>

Here’s what the syntax `foo{x}` lowers to:

```julia
julia> Meta.@lower foo{x}
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope'
1 ─ %1 = Core.apply_type(foo, x)
└── return %1
))))

```

and looking at `Core.apply_type`,

```julia
julia> Core.apply_type
apply_type (built-in function)

```

we see that it is a built-in function and hence cannot have methods added to it

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [May 28, 2021, 5:36am UTC](https://discourse.julialang.org/t/override-foo-x/61969/4 "2021-05-28T05:36:23Z")

</div>

Expanding on this,

```julia
julia> e = :(foo{x})
:(foo{x})

julia> e.head
:curly

julia> e.args
2-element Vector{Any}:
 :foo
 :x

```

so

```julia
macro bar(e)
    v = e.args[1] # vector
    i = e.args[2] # index
    if e.head === :curly
        i = :(length(v) - $i + 1)
    end
    return esc(Expr(:ref, v, i))
end

```

gives you a (inefficient and difficult to understand) specialized syntax:

```julia
julia> foo = 1:10
1:10

julia> x = 2
2

julia> @bar foo[x] # get the second element
2

julia> @bar foo{x} # get the second to last element
9

```

---

<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: [May 28, 2021, 5:37am UTC](https://discourse.julialang.org/t/override-foo-x/61969/5 "2021-05-28T05:37:40Z")

</div>

```julia
julia> struct Foo{T} x::T end

julia> Foo{Int}(x) = Foo{Float64}(Float64(x))

julia> Foo{Int}(3)
Foo{Float64}(3.0)

```
