不允许使用依赖于Julia中类型定义中的整数类型参数的表达式

krc*_*ols 5 templates julia

我想在FixedSizeArrays.Vec {N,T}周围定义一个类型,其中N是type参数的函数:

using FixedSizeArrays

type MT{N}
    x::Vec{N,Int}
    y::Vec{N+1,Int}
end
Run Code Online (Sandbox Code Playgroud)

这会导致错误消息:

ERROR: MethodError: `+` has no method matching +(::TypeVar, ::Int64)
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...)
  +(::Int64, ::Int64)
  +(::Complex{Bool}, ::Real)
  ...
Run Code Online (Sandbox Code Playgroud)

显然,即使在编译时可以知道结果,也不允许使用整数类型参数进行简单算术.有没有人知道这个限制的解决方法?

Mat*_* B. 6

显然,不允许使用整数类型参数的简单算术

是的,这是类型参数的限制.解决此问题的标准方法是使用第二个类型参数.然后,您可以使用内部构造函数强制执行参数不变量:

type MT{N,Np1}
    x::Vec{N,Int}
    y::Vec{Np1,Int}
    function MT(x,y)
        N+1==Np1 || throw(ArgumentError("mismatched lengths; y must be one element longer than x"))
        new(x,y)
    end
end
# Since we define an inner constructor, we must also provide the
# outer constructor to allow calling MT without parameters
MT{N,M}(x::Vec{N,Int}, y::Vec{M,Int}) = MT{N,M}(x,y)
Run Code Online (Sandbox Code Playgroud)

例:

julia> MT(Vec(1,2,3),Vec(1,2,3,4))
MT{3,4}(FixedSizeArrays.Vec{3,Int64}((1,2,3)),FixedSizeArrays.Vec{4,Int64}((1,2,3,4)))

julia> MT(Vec(1,2,3),Vec(1,2,3))
ERROR: ArgumentError: mismatched lengths; y must be one element longer than x
Run Code Online (Sandbox Code Playgroud)