# Typeof(f()) at compile time

**URL:** <https://discourse.julialang.org/t/typeof-f-at-compile-time/27615>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 16, 2019, 1:01pm UTC](https://discourse.julialang.org/t/typeof-f-at-compile-time/27615 "2019-08-16T13:01:16Z")\
**Posts on this page:** 5\
**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 16, 2019, 1:01pm UTC](https://discourse.julialang.org/t/typeof-f-at-compile-time/27615/1 "2019-08-16T13:01:16Z")

</div>

`typeof(f(x))` can be evaluated at compile-time, provided the return type of `f(x)` is inferrable. Why can’t we have a guarantee that this is always the case?

For example, I tried this:

```julia
julia> f(x) = typeof(sin(x))

julia> @code_llvm debuginfo=:none f(5)

define nonnull %jl_value_t addrspace(10)* @julia_f_12432(i64) {
top:
  %1 = sitofp i64 %0 to double
  %2 = call double @julia_sin_12236(double %1)
  ret %jl_value_t addrspace(10)* addrspacecast (%jl_value_t* inttoptr (i64 140650398042128 to %jl_value_t*) to %jl_value_t addrspace(10)*)
}

```

As you can see, the call to `sin` is not optimized away. Even though the type of `sin(5)` is correctly inferred at compile time:

```julia
julia> @code_warntype f(5)
Body::Type{Float64}
1 ─ %1 = (Base.sitofp)(Float64, x)::Float64
│ invoke Base.Math.sin(%1::Float64)
└── return Float64

```

---

<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 16, 2019, 1:17pm UTC](https://discourse.julialang.org/t/typeof-f-at-compile-time/27615/2 "2019-08-16T13:17:26Z")

</div>

Because the two statement (typeof being statically known and sin call be removed) are unrelated. The code you show is a proof that the compiler knows the return type of sin. It’s making the call only because there can be sideeffect and it is right about that. I believe sin can actually throw so even if the compiler is much smarter it still won’t be able to remove the call.

P.s. please use code\_warntype since you seem to be confusing yourself with the llvm code.

---

<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 16, 2019, 1:38pm UTC](https://discourse.julialang.org/t/typeof-f-at-compile-time/27615/3 "2019-08-16T13:38:38Z")

</div>

Ah right! `sin(Inf)` throws. However `exp(x::Float64)` never throws, and there is no side-effect (right?).  
In this case the call to `exp` is not elided either. Why?

```julia
julia> f(x) = typeof(exp(x))
f (generic function with 1 method)

julia> @code_llvm debuginfo=:none f(5.0)

define nonnull %jl_value_t addrspace(10)* @julia_f_12336(double) {
top:
  %1 = call double @julia_exp_12332(double %0)
  ret %jl_value_t addrspace(10)* addrspacecast (%jl_value_t* inttoptr (i64 140549511198736 to %jl_value_t*) to %jl_value_t addrspace(10)*)
}

julia> @code_warntype f(5)
Body::Type{Float64}
1 ─ %1 = (Base.sitofp)(Float64, x)::Float64
│ invoke Base.Math.exp(%1::Float64)
└── return Float64

```

---

<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 16, 2019, 4:48pm UTC](https://discourse.julialang.org/t/typeof-f-at-compile-time/27615/4 "2019-08-16T16:48:22Z")

</div>

First of all, you can still see that the call is in the typed last. You don’t need to look at the llvm it.

And that’s why I said in the sin case it is impossible with any compiler improvement. I don’t know if exp can throw but it’s just showing that the compiler can’t proof the side effect.

---

<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 17, 2019, 2:53pm UTC](https://discourse.julialang.org/t/typeof-f-at-compile-time/27615/5 "2019-08-17T14:53:12Z")

</div>

Thanks!
