# Calculated type parameter warntype

**URL:** <https://discourse.julialang.org/t/calculated-type-parameter-warntype/35440>\
**Category:** General Usage\
**Created:** [March 3, 2020, 4:02am UTC](https://discourse.julialang.org/t/calculated-type-parameter-warntype/35440 "2020-03-03T04:02:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [March 3, 2020, 4:02am UTC](https://discourse.julialang.org/t/calculated-type-parameter-warntype/35440/1 "2020-03-03T04:02:28Z")

</div>

in the following, I’m trying to put some calculated values as type parameters:

```julia
julia> struct A{Plus, Minus}
           A(x, y) = new{x + y, x - y}()
       end

julia> A(2, 3)
A{5,-1}()

julia> @code_warntype A(2, 3)
Variables
  #self#::Type{A}
  x::Int64
  y::Int64

Body::A{_A,_B} where _B where _A
1 ─ %1 = (x + y)::Int64
│ %2 = (x - y)::Int64
│ %3 = Core.apply_type(Main.A, %1, %2)::Type{A{_A,_B}} where _B where _A
│ %4 = %new(%3)::A{_A,_B} where _B where _A
└── return %4

```

how to avoid the above warntype in construction? thanks. 🙏

---

<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:** [March 3, 2020, 4:18am UTC](https://discourse.julialang.org/t/calculated-type-parameter-warntype/35440/2 "2020-03-03T04:18:14Z")

</div>

> [@tomtom](#):
>
> julia\> struct A{Plus, Minus} A(x, y) = new{x + y, x - y}() end

As stated it’s impossible since the compiler, with the info given, has absolutely no idea what `x` and `y` are and therefore couldn’t possibly infor the type you are constructing.

OTOH, without any change, when `x` and `y` are statically known constant propagation should work.

```julia
julia> struct A{Plus, Minus}
           A(x, y) = new{x + y, x - y}()
       end

julia> f() = A(1, 2)
f (generic function with 1 method)

julia> @code_warntype f()
Variables
  #self#::Core.Compiler.Const(f, false)

Body::A{3,-1}
1 ─ %1 = Main.A(1, 2)::Core.Compiler.Const(A{3,-1}(), false)
└── return %1

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [March 3, 2020, 4:31am UTC](https://discourse.julialang.org/t/calculated-type-parameter-warntype/35440/3 "2020-03-03T04:31:24Z")

</div>

seems like using `Var` is the only way?

```julia
julia> struct B{Plus, Minus}
           B(::Val{x}, ::Val{y}) where {x, y} = new{x + y, x - y}()
       end

julia> B(Val(2), Val(3))
B{5,-1}()

julia> @code_warntype B(Val(2), Val(3))
Variables
  #self#::Type{B}
  #unused#@_2::Core.Compiler.Const(Val{2}(), false)
  #unused#@_3::Core.Compiler.Const(Val{3}(), false)

Body::B{5,-1}
1 ─ %1 = ($(Expr(:static_parameter, 1)) + $(Expr(:static_parameter, 2)))::Core.Compiler.Const(5, false)
│ %2 = ($(Expr(:static_parameter, 1)) - $(Expr(:static_parameter, 2)))::Core.Compiler.Const(-1, false)
│ %3 = Core.apply_type(Main.B, %1, %2)::Core.Compiler.Const(B{5,-1}, false)
│ %4 = %new(%3)::Core.Compiler.Const(B{5,-1}(), false)
└── return %4

```

---

<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:** [March 3, 2020, 4:38am UTC](https://discourse.julialang.org/t/calculated-type-parameter-warntype/35440/4 "2020-03-03T04:38:37Z")

</div>

> [@tomtom](#):
>
> seems like using `Var` is the only way?

Sure, if you already have those values in the type parameter you can do whatever you want.

But it’s by all mean not the “only way”. In fact, it’s neither a “way” nor is it the “only” way to achieve the same effect.

It’s not a solution since you are just moving where the type instability is and it won’t help much in real code,

```julia
julia> @code_warntype Val(2)
Variables
  #self#::Type{Val}
  x::Int64

Body::Val{_A} where _A
1 ─ nothing
│ %2 = Core.apply_type(Base.Val, x)::Type{Val{_A}} where _A
│ %3 = (%2)()::Val{_A} where _A
└── return %3

```

It’s not the only way to achieve this since you can go with whatever type parameter you like,

```julia
julia> struct C{P, M}
           C(::NTuple{x}, ::NTuple{y}) where {x, y} = new{x + y, x - y}()
       end

julia> @code_warntype C((1, 2, 3), (2, 3))
Variables
  #self#::Type{C}
  #unused#@_2::Tuple{Int64,Int64,Int64}
  #unused#@_3::Tuple{Int64,Int64}

Body::C{5,1}
1 ─ %1 = ($(Expr(:static_parameter, 1)) + $(Expr(:static_parameter, 2)))::Core.Compiler.Const(5, false)
│ %2 = ($(Expr(:static_parameter, 1)) - $(Expr(:static_parameter, 2)))::Core.Compiler.Const(1, false)
│ %3 = Core.apply_type(Main.C, %1, %2)::Core.Compiler.Const(C{5,1}, false)
│ %4 = %new(%3)::Core.Compiler.Const(C{5,1}(), false)
└── return %4

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [March 3, 2020, 4:50am UTC](https://discourse.julialang.org/t/calculated-type-parameter-warntype/35440/5 "2020-03-03T04:50:28Z")

</div>

oh my god! the fact that `Val(x)` gives warntype is so shocking as it’s so widely used!!! 😵

---

<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:** [March 3, 2020, 6:28am UTC](https://discourse.julialang.org/t/calculated-type-parameter-warntype/35440/6 "2020-03-03T06:28:01Z")

</div>

Most use of it where the argument isn’t a constant or type parameter is wrong.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [March 3, 2020, 7:37am UTC](https://discourse.julialang.org/t/calculated-type-parameter-warntype/35440/7 "2020-03-03T07:37:49Z")

</div>

(deleted). thanks.
