Dispatch on size of MVectors

Hi,

Is there a way to do something like :

using StaticArrays
f(x) = "This is the fallback generic function"
f(x::MVector{M,T}) where {T,M<=10}  = "You passed a MVector with less than 10 ellements"

?

I have the following error :

julia> f(x::MVector{M,T}) where {T,M<=10} = "You passed a MVector with less than 10 ellements"
ERROR: syntax: invalid variable expression in "where" around REPL[26]:1
Stacktrace:
 [1] top-level scope
   @ REPL[26]:1

julia> 

you can’t do computation within type parameters, Julia does NOT have dependent types; here’s a hack:

julia> for M = 0:10
           @eval f(x::MVector{$M,T}) where {T} = "less than 10"
       end

julia> f(@MVector [1,2,3,4,5,6,7,8,9,10,11])
"This is the fallback generic function"

julia> f(@MVector [1,2,3,4,5,6,7,8,9])
"less than 10"
1 Like