# Strange typeof behaviour

**URL:** https://discourse.julialang.org/t/strange-typeof-behaviour/20836
**Category:** New to Julia
**Created:** [February 15, 2019, 8:23pm UTC](https://discourse.julialang.org/t/strange-typeof-behaviour/20836 "2019-02-15T20:23:44Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)
#### Post date: [February 15, 2019, 8:23pm UTC](https://discourse.julialang.org/t/strange-typeof-behaviour/20836/1 "2019-02-15T20:23:44Z")

</div>

Hi All,

Can anyone explain this behaviour?

```julia
struct Foo
  function Foo(f)
    foo = new()
    (foo)(x) = f(x)
    return foo
  end
end
foo = Foo(x->5)
@show typeof(foo)

struct Bar
  function Bar(f)
    bar = new()
    return bar
  end
end
bar = Bar(x->5)
@show typeof(bar)

```

I get, paraphrasing:

```julia
typeof(foo) = getfield(Main, Symbol("#foo#3")){getfield(Main, Symbol("##4#5"))}
typeof(bar) = Bar

```

I’d have thought that `foo` would be of type `Foo`.

Running Julia 1.0

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [February 15, 2019, 8:29pm UTC](https://discourse.julialang.org/t/strange-typeof-behaviour/20836/2 "2019-02-15T20:29:13Z")

</div>

Your `Foo` constructor doesn’t seem to construct a `Foo` object.

---

<div class="post-metadata">

### Author: ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)
#### Post date: [February 15, 2019, 9:50pm UTC](https://discourse.julialang.org/t/strange-typeof-behaviour/20836/3 "2019-02-15T21:50:13Z")

</div>

Sometimes looking at `@code_lowered` can help you understand what is going on -

```julia
julia> @code_lowered Foo(x->5)
CodeInfo(
1 ─ foo = %new(Main.Foo)
│ %2 = Main.:(#foo#7)
│ %3 = (Core.typeof)(f)
│ %4 = (Core.apply_type)(%2, %3)
│ foo = %new(%4, f)
└── return foo
)

```

Note the presence of two calls to `new`; the first one constructs a Foo struct and the second one constructs something else (I have to admit, I’ve no idea what).

It looks to me that you are trying to achieve something like

```julia
julia> struct Foo
           f
       end

julia> (foo::Foo)(x...) = foo.f(x...)

julia> foo = Foo(x -> 5x)
Foo(getfield(Main, Symbol("##7#8"))())

julia> foo(3)
15

```

though it is probably better to parameterise on the function type if you are more concerned about runtime speed over compile times, i.e.,

```julia
julia> struct Foo{F}
           f::F
       end

```

---

<div class="post-metadata">

### Author: ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)
#### Post date: [February 15, 2019, 9:57pm UTC](https://discourse.julialang.org/t/strange-typeof-behaviour/20836/4 "2019-02-15T21:57:18Z")

</div>

That is how I’m currently implementing my working version but was thinking about how to do away with holding `f` on the struct. Hence my inner constructor on `Foo`.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [February 15, 2019, 10:06pm UTC](https://discourse.julialang.org/t/strange-typeof-behaviour/20836/5 "2019-02-15T22:06:43Z")

</div>

How is your struct going to know what function to call if it doesn’t hold onto it? It kinda looks like you’re meaning to write `(::typeof(foo))(x) = 5x` — but that’s not what you want here. That’ll mean that calling _every_ instance of `foo` will result in `5x`, so you definitely don’t want to do that in a constructor.

---

<div class="post-metadata">

### Author: ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)
#### Post date: [February 15, 2019, 10:07pm UTC](https://discourse.julialang.org/t/strange-typeof-behaviour/20836/6 "2019-02-15T22:07:20Z")

</div>

Is it not? I thought I’d be constructing a Foo object with `new`. I must be missing something basic.

---

<div class="post-metadata">

### Author: ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)
#### Post date: [February 15, 2019, 10:08pm UTC](https://discourse.julialang.org/t/strange-typeof-behaviour/20836/7 "2019-02-15T22:08:44Z")

</div>

I was hoping that the function would be bound to the object via magic, to be honest.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [February 15, 2019, 10:08pm UTC](https://discourse.julialang.org/t/strange-typeof-behaviour/20836/8 "2019-02-15T22:08:58Z")

</div>

> [@jcook](#):
>
> Is it not? I thought I’d be constructing a Foo object with `new` . I must be missing something basic.

Your `Foo` constructor is effectively saying:

```julia
  function Foo(f)
    foo = new()
    foo = x -> f(x)
    return foo
  end

```

It’s rebinding `foo` such that it’s an anonymous function.

---

<div class="post-metadata">

### Author: ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)
#### Post date: [February 15, 2019, 10:09pm UTC](https://discourse.julialang.org/t/strange-typeof-behaviour/20836/9 "2019-02-15T22:09:21Z")

</div>

I see!

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [February 15, 2019, 10:11pm UTC](https://discourse.julialang.org/t/strange-typeof-behaviour/20836/10 "2019-02-15T22:11:46Z")

</div>

> [@jcook](#):
>
> I was hoping that the function would be bound to the object via magic, to be honest.

😃

Just remember that Julia dispatches to functions based on their types, not their values. So you can’t define a function (even a call overload) to dispatch on a particular value like you had hoped.

---

<div class="post-metadata">

### Author: ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)
#### Post date: [February 15, 2019, 10:19pm UTC](https://discourse.julialang.org/t/strange-typeof-behaviour/20836/11 "2019-02-15T22:19:29Z")

</div>

Understood 👍

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [February 16, 2019, 12:47am UTC](https://discourse.julialang.org/t/strange-typeof-behaviour/20836/12 "2019-02-16T00:47:32Z")

</div>

In global scope defining a function `foo` or any `const` binding when `foo` already had a value would be an error, but we don’t have `const` in local scope so it’s allowed.
