У меня две проблемы с определением составного типа.
Я следую разделу интерфейсов руководства в 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)
Это дает ошибку (в Юноне):
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
.