On Wed, 2016-05-25 at 11:50, DNF <oyv...@gmail.com> wrote:
> 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 is co-/contravariance: Vector{Int} (say) is not a subtype of
Vector{Any} even though Int<:Any.

Write
hello{T}(v::Vector{T}) = println("Hello")

but if T<:Any then your goodbye function is fine.  If some tighter
constraints are needed then, e.g.

hello{T<:Integer}(v::Vector{T}) = println("Hello")


> 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
>
> On Tuesday, May 24, 2016 at 7:22:53 PM UTC+2, Tamas Papp wrote:
>
>     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

Reply via email to