# Are functions that operate only on types computed at compile time

**URL:** <https://discourse.julialang.org/t/are-functions-that-operate-only-on-types-computed-at-compile-time/89161>\
**Category:** General Usage\
**Tags:** performance, type, parametric-types\
**Created:** [October 23, 2022, 7:38pm UTC](https://discourse.julialang.org/t/are-functions-that-operate-only-on-types-computed-at-compile-time/89161 "2022-10-23T19:38:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HashBrown](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hashbrown/32/43601_2.png) [@HashBrown](https://discourse.julialang.org/u/HashBrown)\
**Post date:** [October 23, 2022, 7:38pm UTC](https://discourse.julialang.org/t/are-functions-that-operate-only-on-types-computed-at-compile-time/89161/1 "2022-10-23T19:38:24Z")

</div>

Suppose I have the following function

```julia
get_first_type(::Type{T}) where T <: Tuple = begin 
    fieldtypes(T)[1]
end

```

If another function calls this function with one of its input type parameters, will this function be computed at compile time since all required information is known (or fail to compile if the tuple type is empty)?

Similarly if one is to use `typeof() ` within a function on one of the function arguments is the call resolved at compile time since the compiler should know the types of all arguments already?

---

<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:** [October 23, 2022, 9:08pm UTC](https://discourse.julialang.org/t/are-functions-that-operate-only-on-types-computed-at-compile-time/89161/2 "2022-10-23T21:08:39Z")

</div>

Generally yes. The compiler is allowed to give up and compute things at run time, but in your example, yes it should generally be done at compile time. You can check what code it is optimizing to by running test cases like

```julia
code_typed(get_first_type, Tuple{Type{Tuple{Float64, Int, String}}})

```

and making sure it’s optimizing everything away

---

<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:** [October 23, 2022, 9:30pm UTC](https://discourse.julialang.org/t/are-functions-that-operate-only-on-types-computed-at-compile-time/89161/3 "2022-10-23T21:30:19Z")

</div>

If you want an example of something that could block this from occurring at compile time, any observable side effects will do:

```julia
julia> const counter = Ref{Int}(0)
Base.RefValue{Int64}(0)

julia> get_first_type_2(::Type{T}) where T <: Tuple = begin 
           counter[] += 1
           fieldtypes(T)[1]
       end
get_first_type (generic function with 1 method)

julia> code_typed(get_first_type_2, Tuple{Type{Tuple{Float64, Int, String}}})
1-element Vector{Any}:
 CodeInfo(
1 ─ %1 = Main.counter::Base.RefValue{Int64}
│ %2 = Base.getfield(%1, :x)::Int64
│ %3 = Base.add_int(%2, 1)::Int64
│ %4 = Main.counter::Base.RefValue{Int64}
│ Base.setfield!(%4, :x, %3)::Int64
│ %6 = $(Expr(:static_parameter, 1))::Type{Tuple{Float64, Int64, String}}
│ %7 = %new(Base.var"#13#14"{DataType}, %6)::Base.var"#13#14"{DataType}
│ %8 = invoke Base.ntupleany(%7::Base.var"#13#14"{DataType}, 3::Int64)::Tuple
│ %9 = Base.getfield(%8, 1, true)::Any
└── return %9
) => Any

```
