Le mercredi 25 mai 2016 à 02:50 -0700, DNF a écrit :
> Is ::Array{Any, 1} the correct annotation?
> >> hello(v::Vector{Any}) = println("Hello")
> >> hello([2,'a'])
> Hello
> >> hello([2,2])
> ERROR: MethodError: no method matching hello(::Array{Int64,1})
> in eval(::Module, ::Any) at
> /usr/local/Cellar/julia/HEAD/lib/julia/sys.dylib:-1
>
> It only works for vectors that are specifically of type Vector{Any}.
> Vector{Int64} is not a subtype of Vector{Any}.
>
> This works, however, even though Vector is not a subtype of
> Vector{Any}:
> >> goodbye(v::Vector) = println("bye, bye")
> goodbye (generic function with 1 method)
> >> goodbye([2,'a'])
> bye, bye
> >>> goodbye([2,2])
> bye, bye
You're hitting type invariance. See
http://docs.julialang.org/en/latest/manual/types/#parametric-composite-types
and look for this term in the mailing list archives.
Regards
> > You are mixing up the constructor and the type syntax. Just use
> > Vector{Any} in the type definition.
> >
> > On Tue, May 24 2016, Andreas Lobinger wrote:
> >
> > > I tend to agree with you, however...
> > >
> > > julia> d = Any[]
> > > 0-element Array{Any,1}
> > >
> > > julia> type d1
> > > name::AbstractString
> > > content::Any[]
> > > end
> > > ERROR: TypeError: d1: in type definition, expected Type{T}, got
> > Array{Any,1}
> > >
> > > On Tuesday, May 24, 2016 at 7:11:50 PM UTC+2, Stefan Karpinski
> > wrote:
> > >
> > > Since Julia 0.4 [] is what you're looking for.
> > >
> > > On Tue, May 24, 2016 at 1:06 PM, Andreas Lobinger <lobi...@gmail
> > .com> wrote:
> > >
> > > Hello colleagues,
> > >
> > > it really feels strange to ask this, but what is the julia
> > equivalent of python's list?
> > >
> > > So.
> > >
> > > 1 can by initialised empty
> > > 2 subject of append and extend
> > > 3 accepting Any entry
> > > 4 foolproof usage in type definition... (my real problem seems
> > to be here)
> > >
> > > Wishing a happy day,
> > >
> > > Andreas
> >