# Should I use Val(N) or Val{N}()?

**URL:** https://discourse.julialang.org/t/should-i-use-val-n-or-val-n/44862
**Category:** General Usage
**Created:** [August 13, 2020, 8:12am UTC](https://discourse.julialang.org/t/should-i-use-val-n-or-val-n/44862 "2020-08-13T08:12:31Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![santiagobadia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/santiagobadia/32/7792_2.png) [@santiagobadia](https://discourse.julialang.org/u/santiagobadia)
#### Post date: [August 13, 2020, 8:12am UTC](https://discourse.julialang.org/t/should-i-use-val-n-or-val-n/44862/1 "2020-08-13T08:12:31Z")

</div>

I wanted to ask whether I can safely use `Val(N)` instead of `Val{N}()`.

In this example

```julia
@code_warntype Val(1)

@code_warntype Val{1}()

```

in the REPL I get completely different outputs. The result is inferrable in  
the second case but not in the first one.

However, in the Julia manual, the use of `Val(N)` is advocated when `N` is passed  
as a parameter, combined with the function-barrier trick.

On the contrary, when I check the output of `@code_warntype foo(zeros(3,4))`

```julia
foo(a::Array{T,N} where T ) where N = Val(N)

```

I get the same result if I replace `Val(N)` with `Val{N}()`. Both are inferrable.

- Could I safely use `Val(N)` instead of `Val{N}()` in my codes?

- If yes, I would like to know why the first example is providing different outputs.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [August 13, 2020, 9:09am UTC](https://discourse.julialang.org/t/should-i-use-val-n-or-val-n/44862/2 "2020-08-13T09:09:17Z")

</div>

Let me attempt an answer. Calling `@code_warntype` on `Val(1)` treats `Val` as the “main” function and 1 as the “input”. Technically, the `Val` function is not type stable. However, inside another function when using `Val(N)` where `N` is a compile-time constant, e.g. a type parameter or the length of a tuple, inter-procedural constant propagation ([https://github.com/JuliaLang/julia/pull/24362](https://github.com/JuliaLang/julia/pull/24362)) kicks in making `Val(N)` infer nicely. Note that the input to the function `Val` when tested directly with `@code_warntype` is not treated as a constant in the above case, hence the type instability.

---

<div class="post-metadata">

### Author: ![santiagobadia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/santiagobadia/32/7792_2.png) [@santiagobadia](https://discourse.julialang.org/u/santiagobadia)
#### Post date: [August 13, 2020, 11:02am UTC](https://discourse.julialang.org/t/should-i-use-val-n-or-val-n/44862/3 "2020-08-13T11:02:34Z")

</div>

I see, inter-procedural constant propagation is the key. Thanks for the answer!

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [August 13, 2020, 2:28pm UTC](https://discourse.julialang.org/t/should-i-use-val-n-or-val-n/44862/4 "2020-08-13T14:28:16Z")

</div>

I’m definitely not an expert in this area, but I think that, at least in recent julia versions, precisely thanks to constant propagation, using `Val` is less of a necessity.

For example, the following infers just fine:

```julia
julia> using Test

julia> f(v::AbstractArray{T, N}) where {T, N} = ntuple(log, N)
f (generic function with 1 method)

julia> @inferred f([1 2; 3 4])
(0.0, 0.6931471805599453)

```

So I’m not completely sure that the performance tips [here](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-value-type-1) to use `Val{N}()` for this scenario are still relevant. (Of course I could be wrong and there could be more complex scenarios in which using `Val` matters.)
