# Typing with "where" statements leads to UndefVarError error?

**URL:** https://discourse.julialang.org/t/typing-with-where-statements-leads-to-undefvarerror-error/36767
**Category:** New to Julia
**Tags:** question, type
**Created:** [March 30, 2020, 11:26pm UTC](https://discourse.julialang.org/t/typing-with-where-statements-leads-to-undefvarerror-error/36767 "2020-03-30T23:26:32Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Mikael\_Ohman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikael_ohman/32/13609_2.png) [@Mikael\_Ohman](https://discourse.julialang.org/u/Mikael_Ohman)
#### Post date: [March 30, 2020, 11:26pm UTC](https://discourse.julialang.org/t/typing-with-where-statements-leads-to-undefvarerror-error/36767/1 "2020-03-30T23:26:32Z")

</div>

Hi all. I’m very new to Julia. Using 1.2.0 in these examples.

I hit upon a little snag when i wanted to partially specify the return types for a function. E.g, specifying that the output will be `SVector{N,Int}` for any `N` , where (unlike arrays) the type was given as the second type parameter.  
However, the typical `where` approach doesn’t seem to work here:

```julia
function f()::SVector{N,Int} where {N}
    return SA[1,2,3]
end

```

```nohighlight
julia> f()
UndefVarError: N not defined

Stacktrace:
 [1] f() at ./In[27]:2
 [2] top-level scope at In[27]:6

```

though, strangely, the one-liner version works;

```julia
g()::SVector{N,Int} where {N} = SA[1,2,3]

```

```nohighlight
julia> g()
3-element SArray{Tuple{3},Int64,1,3} with indices SOneTo(3):
 1
 2
 3

```

Now, even stranger (to me) is that this works:

```julia
function f(x::SVector{N,Int})::SVector{N,Int} where {N}
    return 2 .* x
end

```

```nohighlight
julia> f(SA[1,2,3])
3-element SArray{Tuple{3},Int64,1,3} with indices SOneTo(3):
 2
 4
 6

```

whereas the one-liner equivalent now doesn’t work instead:

```julia
g(x::SVector{N,Int})::SVector{N,Int} where {N} = 2 .* x

```

```nohighlight
UndefVarError: N not defined

Stacktrace:
 [1] top-level scope at In[18]:1

```

in fact, it can’t even be defined unlike the original `f()`.

In my particular usecase, I can probably just drop the return type info for my generic functions anyway, but I thought i had i grasp on the typesystem until I hit upon this.  
Is this a bug or shortcoming, or am I simply doing it wrong?  
Thanks

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [March 30, 2020, 11:44pm UTC](https://discourse.julialang.org/t/typing-with-where-statements-leads-to-undefvarerror-error/36767/2 "2020-03-30T23:44:33Z")

</div>

In general, you shouldn’t specify return types. In almost all cases, there is no benefit for doing so.

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [March 31, 2020, 12:35am UTC](https://discourse.julialang.org/t/typing-with-where-statements-leads-to-undefvarerror-error/36767/3 "2020-03-31T00:35:13Z")

</div>

On rare occasion it is useful, so it would still be good to get an answer.

---

<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 31, 2020, 1:13am UTC](https://discourse.julialang.org/t/typing-with-where-statements-leads-to-undefvarerror-error/36767/4 "2020-03-31T01:13:15Z")

</div>

You are seeing the difference between `f()::(SVector{N,Int} where {N})` and `(f()::SVector{N,Int}) where {N}`.

For `f()::(SVector{N,Int} where {N})`, `SVector{N,Int} where {N}` is a well defined (abstract) type so there’s no error.

For `(f()::SVector{N,Int}) where {N}`, `N` is a parameter of the function that is used in the return type declaration so it must be deducted from the argument for it to work. You get an error in that case since `N` cannot be deducted.

Finally, the two function definition syntax behaves slightly differently in this case, and that’s why you only get error in one and not the other. You can see the difference by dumping the expression or use `Meta.show_sexpr` or any number of other tools that doesn’t try to be smart about adding parenthesis during printing. Adding appropriate parenthesis should fix the issue.

* * *

And for completeness

`(f(x::SVector{N,Int})::SVector{N,Int}) where {N}` works though the return type is a concrete type of `SVector{N,Int}` with `N` taking a specific value rather than an abstract type.

and

`g(x::SVector{N,Int})::(SVector{N,Int} where {N})` cannot be defined since you are using a `SVector{N,Int}` as argument type but the `N` is not defined anywhere.

---

<div class="post-metadata">

### Author: ![Mikael\_Ohman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikael_ohman/32/13609_2.png) [@Mikael\_Ohman](https://discourse.julialang.org/u/Mikael_Ohman)
#### Post date: [March 31, 2020, 10:04am UTC](https://discourse.julialang.org/t/typing-with-where-statements-leads-to-undefvarerror-error/36767/5 "2020-03-31T10:04:19Z")

</div>

Thanks yuyichao!

So, if I wanted to specify an SVector integer input of length N and output of any length M, i would have to do

```julia
function f(x::SVector{N,Int})::(SVector{M,Int} where M) where N
    return SA[1,2,3,4]
end
f(SA[1,2,3])

```

but I do see the advantage of this, as N becomes a variable for the output type

```julia
function double_up(x::SVector{N,Int})::SVector{2*N,Int} where N
    return vcat(x, x)
end

```

(In my particular usecase, I think I will just be return types completely)

Best regards, Mikael
