# Why typeof is built-in?

**URL:** <https://discourse.julialang.org/t/why-typeof-is-built-in/14327>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 30, 2018, 6:53pm UTC](https://discourse.julialang.org/t/why-typeof-is-built-in/14327 "2018-08-30T18:53:21Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [August 30, 2018, 6:53pm UTC](https://discourse.julialang.org/t/why-typeof-is-built-in/14327/1 "2018-08-30T18:53:22Z")

</div>

One could define `typeof(x::T) where T = T`. So why is it built-in instead of coded in Julia?

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [August 30, 2018, 7:04pm UTC](https://discourse.julialang.org/t/why-typeof-is-built-in/14327/2 "2018-08-30T19:04:47Z")

</div>

You really don’t want it to be overloadable, because then people might try that. For that purpose a builtin is just fine. Not to mention that the compiler can generate efficient code for `typeof` even when it doesn’t know the type of the variable while your proposed definition would fall back to a dynamic dispatch (that then internally calls `jl_typeof` in C). That’s all optimizable of course, but there’s just no benefit to making it a generic function.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [August 30, 2018, 7:11pm UTC](https://discourse.julialang.org/t/why-typeof-is-built-in/14327/3 "2018-08-30T19:11:45Z")

</div>

Recently I had a similar issue with `ifelse`. What is the benefit of it being built-in? Is it because then the compiler can avoid branching and if it were overloadable it would not be possible?

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [August 30, 2018, 7:12pm UTC](https://discourse.julialang.org/t/why-typeof-is-built-in/14327/4 "2018-08-30T19:12:32Z")

</div>

No, `ifelse` is horrible and will go away.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [August 30, 2018, 7:19pm UTC](https://discourse.julialang.org/t/why-typeof-is-built-in/14327/5 "2018-08-30T19:19:25Z")

</div>

Thanks! Good to know, because this is what I thought it should be, but low level stuff is often tricky.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [August 30, 2018, 7:31pm UTC](https://discourse.julialang.org/t/why-typeof-is-built-in/14327/6 "2018-08-30T19:31:39Z")

</div>

Can you eliminate it before Julia 2.0?

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [August 30, 2018, 7:32pm UTC](https://discourse.julialang.org/t/why-typeof-is-built-in/14327/7 "2018-08-30T19:32:24Z")

</div>

Sure, just define `ifelse(b, x, y) = b ? x : y`.

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [August 30, 2018, 8:04pm UTC](https://discourse.julialang.org/t/why-typeof-is-built-in/14327/8 "2018-08-30T20:04:23Z")

</div>

For introductory users, the ternary operator is pretty tough for them to mentally parse… So I hope that this sort of definition stays around in one form or another (for when short circuiting isn’t needed, and clarity is at a premium)

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [August 30, 2018, 8:52pm UTC](https://discourse.julialang.org/t/why-typeof-is-built-in/14327/9 "2018-08-30T20:52:37Z")

</div>

Honestly I think `condition ? value : othervalue` is very clear.

---

<div class="post-metadata">

**Author:** ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)\
**Post date:** [August 30, 2018, 9:01pm UTC](https://discourse.julialang.org/t/why-typeof-is-built-in/14327/10 "2018-08-30T21:01:17Z")

</div>

Why is it horrible?

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [August 30, 2018, 9:09pm UTC](https://discourse.julialang.org/t/why-typeof-is-built-in/14327/11 "2018-08-30T21:09:10Z")

</div>

Ah you mean no longer making it a builtin.

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [August 30, 2018, 9:09pm UTC](https://discourse.julialang.org/t/why-typeof-is-built-in/14327/12 "2018-08-30T21:09:46Z")

</div>

Primarily because type inference/optimizations can’t use the conditional to reason about the arguments (e.g. type inference may know that the condition implies something about the types of the arguments, but since the arguments are evaluated first it can’t use that to optimize without turning ifelse back into `?:`).

---

<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 30, 2018, 10:11pm UTC](https://discourse.julialang.org/t/why-typeof-is-built-in/14327/13 "2018-08-30T22:11:16Z")

</div>

I thought that the purpose of `ifelse` was to reap performance benefits by avoiding branching, and that for example vectorization was prevented by branching. Is that no longer relevant?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 30, 2018, 10:20pm UTC](https://discourse.julialang.org/t/why-typeof-is-built-in/14327/14 "2018-08-30T22:20:45Z")

</div>

The hardest thing for the vectorizer about branches is to proof that both branches are free of side effects. The branch itself isn’t an issue (at least on latest llvm). So a eagerly evaluating branch is still somewhat useful but it can just be implemented as a branch and an intrinsic isn’t useful.
