# Typeof a function

**URL:** <https://discourse.julialang.org/t/typeof-a-function/65865>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 5, 2021, 9:00am UTC](https://discourse.julialang.org/t/typeof-a-function/65865 "2021-08-05T09:00:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Judd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/judd/32/27866_2.png) [@Judd](https://discourse.julialang.org/u/Judd)\
**Post date:** [August 5, 2021, 9:00am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/1 "2021-08-05T09:00:06Z")

</div>

What does the output mean? #1, #2, and couting.

```julia
julia> typeof((a::Int64) -> a + 1)
var"#1#2"
julia> typeof((a::Int64) -> a + 1)
var"#3#4"

```

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [August 5, 2021, 9:03am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/2 "2021-08-05T09:03:23Z")

</div>

This is a `gensym`ed name.  
It means nothing.  
It is just a unique name for an otherwise anonymous function, that is certain not to clash,

---

<div class="post-metadata">

**Author:** ![Judd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/judd/32/27866_2.png) [@Judd](https://discourse.julialang.org/u/Judd)\
**Post date:** [August 6, 2021, 1:59am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/3 "2021-08-06T01:59:39Z")

</div>

What is the `type` of a anonymous function? Maybe `Function`? I have failed to find relative information in the documentation.

---

<div class="post-metadata">

**Author:** ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)\
**Post date:** [August 6, 2021, 2:06am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/4 "2021-08-06T02:06:05Z")

</div>

The type of an anonymous function depends on the anonymous function. A location that defines an anonymous function generates a new type with a name generated with help of the `gensym` function.

---

<div class="post-metadata">

**Author:** ![Judd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/judd/32/27866_2.png) [@Judd](https://discourse.julialang.org/u/Judd)\
**Post date:** [August 6, 2021, 2:35am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/5 "2021-08-06T02:35:26Z")

</div>

Do you mean that the `type` of an anonymous function depends on **where** it is defined (line number, or something else) ?

Any documentation for this?

---

<div class="post-metadata">

**Author:** ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)\
**Post date:** [August 6, 2021, 5:44am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/6 "2021-08-06T05:44:56Z")

</div>

The name of the type depends on how many times the function `gensym` has been called before it. Several things in the language use the `gensym` function to generate unique symbols.

The fields within the generated type on the other hand depend on what variables were closed over by the anonymous function.

---

<div class="post-metadata">

**Author:** ![Judd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/judd/32/27866_2.png) [@Judd](https://discourse.julialang.org/u/Judd)\
**Post date:** [August 6, 2021, 6:00am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/7 "2021-08-06T06:00:42Z")

</div>

So, the `type` of each anonymous function is an **unique** type represented by an unique symbol which itself does not carry any information on the type of the function.

I am curious that if this is just a temporary implementation choice, or a rational design decision like using `*` for concatenating string.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [August 6, 2021, 6:01am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/8 "2021-08-06T06:01:30Z")

</div>

This is not unique to anonymous functions, every function has its own unique type:

```julia
julia> typeof(sin)
typeof(sin)

```

It’s just that anyonymous functions, as the name suggests, don’t have a name, so they get a gensymmed one.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 6, 2021, 6:01am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/9 "2021-08-06T06:01:57Z")

</div>

In julia, each function has its own type and subtypes `Function`. For example, the type of the `sin` function is `typeof(sin)` (for lack of a better name).

Now, anonymous functions don’t have a name, but we still need some somewhat human readable identifier to refer to - that’s where that generated name comes in. Notably, these anonymous functions also each have their own type, just like regular named functions.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 6, 2021, 6:04am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/10 "2021-08-06T06:04:51Z")

</div>

> [@Judd](#):
>
> I am curious that if this is just a temporary implementation choice, or a rational design decision like using `*` for concatenating string.

If you have two anonymous functions with the exact same syntax, how would you distinguish them? Julia doesn’t have argument types and return types of functions as part of the type of a function, like Haskell for example (which also wouldn’t help, since we assumed syntax was the exact same and the functions can still be distinct objects in spite of being functionally identical). The only way to make it clear to human readers is to give them a generated name internally.

---

<div class="post-metadata">

**Author:** ![Judd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/judd/32/27866_2.png) [@Judd](https://discourse.julialang.org/u/Judd)\
**Post date:** [August 6, 2021, 6:10am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/11 "2021-08-06T06:10:25Z")

</div>

This looks like that `typeof` is not evaluated at all. 😀

> [@nilshg](#):
>
> …
> 
> ```julia
> julia> typeof(sin)
> typeof(sin)
> 
> ```

---

<div class="post-metadata">

**Author:** ![Judd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/judd/32/27866_2.png) [@Judd](https://discourse.julialang.org/u/Judd)\
**Post date:** [August 6, 2021, 6:12am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/12 "2021-08-06T06:12:05Z")

</div>

Roughly, `typeof(sin)` is the same as `typeof(cos)`. Right?

> [@Sukera](#):
>
> In julia, each function has its own type and subtypes `Function`. For example, the type of the `sin` function is `typeof(sin)` (for lack of a better name).

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 6, 2021, 6:15am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/13 "2021-08-06T06:15:41Z")

</div>

No. `typeof(sin) !== typeof(cos)`. They only share a common supertype, `Function`.

The type of any function `f` is just written/printed like `typeof(f)` to make it clear we’re talking about the type of `f` and not `f` itself. You can pass functions around like any other object, after all.

---

<div class="post-metadata">

**Author:** ![Judd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/judd/32/27866_2.png) [@Judd](https://discourse.julialang.org/u/Judd)\
**Post date:** [August 6, 2021, 6:23am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/15 "2021-08-06T06:23:17Z")

</div>

Julia redefined the `type` of functions?

Would you tell me where I can find relative information in Julia docs?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [August 6, 2021, 6:24am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/16 "2021-08-06T06:24:13Z")

</div>

Nothing is redefined here. Maybe this StackOverflow answer helps:

[https://stackoverflow.com/questions/52351852/puzzling-results-for-julia-typeof](https://stackoverflow.com/questions/52351852/puzzling-results-for-julia-typeof)

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 6, 2021, 6:26am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/17 "2021-08-06T06:26:15Z")

</div>

No, julia didn’t redefine anything - that’s literally the type of that specific function.

There’s the abstract type `Function` and its concrete subtypes, e.g. `typeof(sin)`, `typeof(cos)`, `typeof(tan)` etc. which are all distinct types, each with a singleton instance (in these cases, `sin` for `typeof(sin)`, `cos` for `typeof(cos)`…)

---

<div class="post-metadata">

**Author:** ![Judd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/judd/32/27866_2.png) [@Judd](https://discourse.julialang.org/u/Judd)\
**Post date:** [August 6, 2021, 7:17am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/18 "2021-08-06T07:17:52Z")

</div>

Now IMHO, I have got a better understanding of `typeof` functions. If anything wrong, please correct me.

1. Why I said that Julia redefined `type` of functions?

2. Why the redefinition?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 6, 2021, 7:43am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/19 "2021-08-06T07:43:13Z")

</div>

Julia, unlike Haskell, doesn’t have function type. The input and output types of a function is not part of the typing system (they are known to compiler for compilation purpose). As others have said here, the type of a function `f` is simply `typeof(f)` and that’s the end of the story. There’s no information regarding the possible or actual input types and output types of `f` here.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 6, 2021, 7:47am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/20 "2021-08-06T07:47:36Z")

</div>

Ah, I think I see where the confusion comes from! In most other programming languages, a “function” is usually the name **and** the associated argument types. In julia, that’s not the case - a function is just a name (though it’s usually associated with a namespace as well, we can ignore that here for now). As soon as you associate specific argument types (i.e. a signature) to a function, you have a method of that function.

For example, consider the `sin` example. In Base julia, `sin` is a single function, but has many methods:

```julia
julia> methods(sin)                     
# 13 methods for generic function "sin":

```

one of those handles arguments of type `Float64`:

```julia
julia> sin(1.0)   
0.8414709848078965

```

another handles complex arguments:

```julia
julia> sin(1im)           
0.0 + 1.1752011936438014im

```

These are still the same function though! In julia a “function” is mostly a common name for a certain operation, paired with specializations for specific argument types we call “method”. Crucially, the function alone _does not give julia any knowledge about its argument types or the type of the object that will be returned from it_. That is method specific, not function specific (and we can’t pass around specific methods of a function because we don’t know _which_ method will be called until it is called).

* * *

Now, since we want to possibly pass `sin` (or any other function, really) around and may want to specialize how a function works based on the function it gets passed in, we have to make each function have its own type. This will be a little clearer printed in 1.7:

```julia
julia> typeof(sin)                                               
typeof(sin) (singleton type of function sin, subtype of Function)

```

Why is this useful? Well, sometimes we can take advantage of properties (e.g. commutativity) of certain functions to do something smarter. `reduce(+, array)` for example does Kahan summation on arrays of floating point arguments, which reduces accumulation error. If `reduce` couldn’t distinguish `+` from `*` (or any other binary operation, really), it wouldn’t necessarily be allowed to do that.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 6, 2021, 8:04am UTC](https://discourse.julialang.org/t/typeof-a-function/65865/21 "2021-08-06T08:04:21Z")

</div>

> [@Sukera](#):
>
> `reduce(+, array)` for example does Kahan summation

Don’t you mean pairwise summation? That’s what `sum` does, at least.
