# Argument types not inferred when constrained in function signature

**URL:** <https://discourse.julialang.org/t/argument-types-not-inferred-when-constrained-in-function-signature/19020>\
**Category:** Internals & Design\
**Tags:** bug, inference\
**Created:** [December 27, 2018, 3:48am UTC](https://discourse.julialang.org/t/argument-types-not-inferred-when-constrained-in-function-signature/19020 "2018-12-27T03:48:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cnliao](https://avatars.discourse-cdn.com/v4/letter/c/ce73a5/32.png) [@cnliao](https://discourse.julialang.org/u/cnliao)\
**Post date:** [December 27, 2018, 3:48am UTC](https://discourse.julialang.org/t/argument-types-not-inferred-when-constrained-in-function-signature/19020/1 "2018-12-27T03:48:19Z")

</div>

It is a curious case when I tried to see how julia handles type constraints:

```julia
julia> f(a::A, b::B) where{B<:Tuple{C}} where {A,C<:A} = (A, B, C)
f (generic function with 1 method)

julia> @code_warntype f(1, (2,))
Body::Tuple{DataType,DataType,DataType}
1 ─ %1 = (Core.tuple)($(Expr(:static_parameter, 1)), $(Expr(:static_parameter, 3)), $(Expr(:static_parameter, 2)))::Core.Compiler.Const((Int64, Tuple{Int64}, Int64), false)
└── return %1

julia> @code_warntype f(1, ("2",))
Body::Tuple{Type{A} where A<:Union{Int64, String},DataType,DataType}
1 ─ %1 = (Core.tuple)($(Expr(:static_parameter, 1)), $(Expr(:static_parameter, 3)), $(Expr(:static_parameter, 2)))::Core.Compiler.PartialTuple(Tuple{Type{A} where A<:Union{Int64, String},DataType,DataType}, Any[Type{A} where A<:Union{Int64, String}, Const(Tuple{String}, false), Const(String, false)])
└── return %1

julia> f(1, ("2",)) # can be (Any, Tuple{Int}, Int) ?
ERROR: UndefVarError: A not defined
Stacktrace:
 [1] f(::Int64, ::Tuple{String}) at ./REPL[1]:1
 [2] top-level scope at none:0

```

Same on master and v1.0.3.  
Is it a bug? Or better error message needed?

---

<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:** [December 27, 2018, 4:02am UTC](https://discourse.julialang.org/t/argument-types-not-inferred-when-constrained-in-function-signature/19020/2 "2018-12-27T04:02:53Z")

</div>

```julia
 f(a::A, b::B) where{A, C<:A, B<:Tuple{C}} = (A, B, C)

```

Edit: this is not a fix

---

<div class="post-metadata">

**Author:** ![cnliao](https://avatars.discourse-cdn.com/v4/letter/c/ce73a5/32.png) [@cnliao](https://discourse.julialang.org/u/cnliao)\
**Post date:** [December 27, 2018, 4:12am UTC](https://discourse.julialang.org/t/argument-types-not-inferred-when-constrained-in-function-signature/19020/3 "2018-12-27T04:12:47Z")

</div>

That does not work for me. Did you try `f(1,("2",))` ? What julia version are you on?

---

<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:** [December 27, 2018, 4:24am UTC](https://discourse.julialang.org/t/argument-types-not-inferred-when-constrained-in-function-signature/19020/4 "2018-12-27T04:24:13Z")

</div>

Oops sorry, I tried it on `(1, (2,))` but yours also works there. Mine doesn’t work on `(1, ("2",))`.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [December 27, 2018, 7:50am UTC](https://discourse.julialang.org/t/argument-types-not-inferred-when-constrained-in-function-signature/19020/5 "2018-12-27T07:50:20Z")

</div>

This doesn’t feel like a bug to me but the error message is in any case strange.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [December 28, 2018, 4:02pm UTC](https://discourse.julialang.org/t/argument-types-not-inferred-when-constrained-in-function-signature/19020/6 "2018-12-28T16:02:40Z")

</div>

> [@cnliao](#):
>
> ```
> # can be (Any, Tuple{Int}, Int) ?
> 
> ```

It could be, but it’s not a unique solution. The static-parameters only get assigned a value if they are uniquely constrained (sorry, I’m not immediately sure where to point to documentation of this). There’s been a call for more informative error messages for related cases, if someone wanted to investigate further ([Better error messages for UndefVarError · Issue #29313 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/29313))
