我在定义复合类型时遇到两个问题。
我按照Julia v0.6中手册的接口部分进行操作。我已经构建了一个简单的最小(非)工作示例:
mutable struct Group <: AbstractArray{Integer, 1}
x1::Int64
x2::Int64
end
Base.size(g::Group) = 2
Base.length(g::Group) = 2
Base.eltype(::Type{Group}) = Int64
Base.getindex(g::Group, i::Int64) = getindex([g.x1, g.x2], i)
Base.setindex!(g::Group, v, i::Int64) = Base.setindex!(g, v, i)
Base.IndexStyle(::Type{<:Group}) = IndexLinear()
Base.start(g::Group) = 1
Base.next(g::Group, s::Int) = g[s], s+1
Base.done(g::Group, s::Int) = (s > length(g))
Base.show(io::IO, g::Group) = print("\t", string(g.x1), "\n\t", string(g.x2))
它很有希望:
julia> g = Group(1, 2);
julia> g.x1 = 42
42
julia> g.x1 = 99
99
julia> length(g)
2
julia> for i in h
@show i
end
i = 99
i = 2
可是:
g = Group(1, 2)
这给出了错误(在Juno中):
Error displaying Group: MethodError: no method matching inds2string(::Base.OneTo{Int64})[0m
Closest candidates are:
inds2string([91m::Tuple{Vararg{AbstractUnitRange,N}} where N[39m) at show.jl:1568
_summary(::Group, ::Base.OneTo{Int64}) at show.jl:1573
#showarray#265(::Bool, ::Function, ::IOContext{Base.AbstractIOBuffer{Array{UInt8,1}}}, ::Group, ::Bool) at show.jl:1685
(::Atom.##17#18{Group})(::Base.AbstractIOBuffer{Array{UInt8,1}}) at display.jl:17
#sprint#230(::Void, ::Function, ::Int64, ::Function) at io.jl:66
Type at types.jl:39 [inlined]
Type at types.jl:40 [inlined]
render(::Juno.Editor, ::Group) at display.jl:19
render′(::Juno.Editor, ::Group) at errors.jl:105
(::Atom.##103#108{String})() at eval.jl:91
macro expansion at eval.jl:87 [inlined]
(::Atom.##100#105{Dict{String,Any}})() at task.jl:80
但:
help?> inds2string
search:
Couldn't find inds2string
Perhaps you meant randstring
No documentation found.
Binding inds2string does not exist.
第二个问题是因为我想使用广播:
julia> sin.(g)
然后我收到了这个错误:
MethodError: Cannot `convert` an object of type Base.OneTo{Int64} to an
object of type CartesianRange
This may have arisen from a call to the constructor CartesianRange(...),
since type constructors fall back to convert methods.
CartesianRange(::Base.OneTo{Int64}) at sysimg.jl:77
broadcast_c at broadcast.jl:314 [inlined]
broadcast(::Function, ::Group) at broadcast.jl:455
include_string(::String, ::String) at loading.jl:522
include_string(::String, ::String, ::Int64) at eval.jl:30
include_string(::Module, ::String, ::String, ::Int64, ::Vararg{Int64,N}
where N) at eval.jl:34
(::Atom.##102#107{String,Int64,String})() at eval.jl:82
withpath(::Atom.##102#107{String,Int64,String}, ::Void) at utils.jl:30
withpath(::Function, ::String) at eval.jl:38
hideprompt(::Atom.##101#106{String,Int64,String}) at repl.jl:67
macro expansion at eval.jl:80 [inlined]
(::Atom.##100#105{Dict{String,Any}})() at task.jl:80
这不是很有帮助。我显然需要在Group
的定义中添加其他内容。
答案 0 :(得分:2)
问题是你的size
方法返回一个整数; size
应该返回一个整数元组,每个维度的长度。在这种情况下,当我们定义一个向量时,它应该返回一个1元组,如:
Base.size(::Group) = (2, )
不幸的是,这会导致下游有些模糊的错误消息。
其他一些可能有用的评论:
你的getindex有效,但分配了一个不必要的Vector
,或许类似以下内容会更好:
function Base.getindex(g::Group, i::Int)
if i == 1
return g.x1
elseif i == 2
return g.x2
else
throw(BoundsError(g, i))
end
end
您对setindex!
的定义会调用自身并导致StackOverflow
错误,或许类似以下情况会更好:
function Base.setindex!(g::Group, v, i)
println("setindex!(:Group, v = $v, i = $i)")
if i == 1
g.x1 = v
elseif i == 2
g.x2 = v
else
throw(BoundsError(g, i))
end
end