# Constant propagation of function of type

**URL:** https://discourse.julialang.org/t/constant-propagation-of-function-of-type/92963
**Category:** General Usage
**Tags:** question
**Created:** [January 14, 2023, 2:39pm UTC](https://discourse.julialang.org/t/constant-propagation-of-function-of-type/92963 "2023-01-14T14:39:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tictaccat](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tictaccat](https://discourse.julialang.org/u/tictaccat)
#### Post date: [January 14, 2023, 2:39pm UTC](https://discourse.julialang.org/t/constant-propagation-of-function-of-type/92963/1 "2023-01-14T14:39:14Z")

</div>

Suppose I have a function

```julia
function f(args...)
     X = g(typeof(args))
     # do stuff with X and args..
end

```

The function `g` is fairly complicated (performs a recursive walk of a tuple type-type), but ultimately it is pure and returns another type `X`. Assuming that the type of `args` is inferred, can I count on the compiler to constant prop the computation of `X` away so that it does not appear in the compiled code? Or will this only work for very simple `g`?

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [January 16, 2023, 10:30pm UTC](https://discourse.julialang.org/t/constant-propagation-of-function-of-type/92963/2 "2023-01-16T22:30:21Z")

</div>

Usually I would expect this to work, but it depends on exactly what your `g` is. I’d suggest you try it. If it doesn’t work, provide more details and people can try to help.

---

<div class="post-metadata">

### Author: ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)
#### Post date: [January 16, 2023, 11:30pm UTC](https://discourse.julialang.org/t/constant-propagation-of-function-of-type/92963/3 "2023-01-16T23:30:01Z")

</div>

As a test of whether the compiler is doing what you expect or not, you might consider writing a `@generated` function and comparing the output/performance/lowered code of the two functions. The way that I think about `@generated` functions, which is probably not the most informed or correct, is this template:

```julia
@generated function f(x::X, y::Y, [...]) where{X, Y, [...]}
  # functions and operations with only type information:
  Z = some_types_only_function(X, Y, [other_types...])
  W = other_types_only_function([...])
  quote
    # "normal" information available here, as well as $Z, $W, etc.
    [...] #stuff 
  end
end

```

Maybe this would be a useful tool to explore?

---

<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: [January 16, 2023, 11:34pm UTC](https://discourse.julialang.org/t/constant-propagation-of-function-of-type/92963/4 "2023-01-16T23:34:58Z")

</div>

> Assuming that the type of `args` is inferred, can I count on the compiler to constant prop the computation of `X` away so that it does not appear in the compiled code?

Generally yes for things that are specialized on (like types). Use Cthulhu to see what the compiler is actually doing with your code.
