Tengo dos problemas para definir un tipo compuesto.
Estoy siguiendo la sección de interfaces del manual en Julia v0.6. He creado un ejemplo de funcionamiento mínimo (no) simple:
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))
Es prometedor:
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
Pero:
g = Group(1, 2)
Esto da el error (en 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
pero:
help?> inds2string
search:
Couldn't find inds2string
Perhaps you meant randstring
No documentation found.
Binding inds2string does not exist.
El segundo problema es porque me gustaría usar la transmisión:
julia> sin.(g)
entonces me sale este error:
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
lo cual no es muy útil. Obviamente, necesito agregar algo más a la definición de Group
.
El problema es que su size
método devuelve un número entero; size
debe devolver una tupla de números enteros con las longitudes de cada dimensión. En este caso, cuando definimos un vector, debería devolver una tupla 1, algo como:
Base.size(::Group) = (2, )
Desafortunadamente, esto conduce a mensajes de error un tanto oscuros en sentido descendente.
Algunos otros comentarios que pueden ser útiles:
Su getindex funciona, pero asigna un innecesario Vector
, quizás algo como lo siguiente sería mejor:
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
Su definición de se setindex!
llama a sí misma y daría lugar a un StackOverflow
error, quizás algo como lo siguiente sería mejor:
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
Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.
En caso de infracción, por favor [email protected] Eliminar
Déjame decir algunas palabras