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:
function f()::SVector{N,Int} where {N}
return SA[1,2,3]
end
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;
g()::SVector{N,Int} where {N} = SA[1,2,3]
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:
function f(x::SVector{N,Int})::SVector{N,Int} where {N}
return 2 .* x
end
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:
g(x::SVector{N,Int})::SVector{N,Int} where {N} = 2 .* x
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