The fact that append! grows the array on failure seems like a bug nonetheless. If convert throws it seems preferable to leave the array as is. I'll file an issue.
Simon On Thursday, July 17, 2014 9:34:21 AM UTC-4, Jacob Quinn wrote: > > Hi Jan, > > You have your syntax a little mixed up. The usage of: > > Type[] > > actually declares an empty array with element type of `Type`. So you're > first line: > > x = Array[] > > is actually creating an array of arrays. > > Likewise, you're seeing the error in > > Array[1] > > Because you're trying to put an Int[1] array into an array of arrays (and > julia doesn't know how to make that conversion). > > The last error is unfortunate, because it seems that the call to `append!` > is allocating space for the array you're appending but then fails when > actually trying to put the 2nd array into the newly allocated space. > Because `append!` is mutating, you're left with your original array mutated > with the extra space, with the #undef. > > I think the semantics you're looking for are as follows: > > x = Int[] # declares an empty 1-d array with element type of `Int` > > y = [1, 2, 3] # literal syntax for creating an array with elements, (1, > 2, 3). Type inference figures out that the elements are of type `Int` > > append!(x,y) # mutates the `x` array by appending all the elements of y > to it; this works because they're both of the same type > > push!(x, 5) # adds a single element, 5, to the `x` array > > Feel free to read through the section in teh manual on arrays to get a > better feel for what's going on. > > http://docs.julialang.org/en/latest/manual/arrays/ > > Cheers, > > -Jacob > > > On Thu, Jul 17, 2014 at 8:21 AM, Jan Strube <jan.s...@gmail.com > <javascript:>> wrote: > >> >> >> Hi List! >> >> I'm a particle physicist just getting started with some julia. I've been >> using some python in the past but now I'm trying to use analyzing some lab >> data as an excuse to learn Julia. >> So far it looks like I'm going to stick with it for a while. >> >> I've been trying to play with basic image analysis, and I've come across >> the following behavior: append! complains that it doesn't find a suitable >> method for my call signature, but it does append an #undef. Is this >> intended? >> >> Please see below for a session. >> >> >> >> _ _ _(_)_ | A fresh approach to technical computing >> (_) | (_) (_) | Documentation: http://docs.julialang.org >> _ _ _| |_ __ _ | Type "help()" to list help topics >> | | | | | | |/ _` | | >> | | |_| | | | (_| | | Version 0.3.0-rc1 (2014-07-14 02:04 UTC) >> _/ |\__'_|_|_|\__'_| | >> |__/ | x86_64-apple-darwin13.3.0 >> >> julia> x = Array[] >> 0-element Array{Array{T,N},1} >> >> julia> append!(x, Array[1]) >> ERROR: `convert` has no method matching convert(::Type{Array{T,N}}, >> ::Int64) >> in getindex at array.jl:121 >> >> julia> x >> 0-element Array{Array{T,N},1} >> >> julia> append!(x, Array[[1]]) >> 1-element Array{Array{T,N},1}: >> [1] >> >> julia> append!(x, [1]) >> ERROR: `convert` has no method matching convert(::Type{Array{T,N}}, >> ::Int64) >> in copy! at abstractarray.jl:197 >> in append! at array.jl:475 >> >> julia> x >> 2-element Array{Array{T,N},1}: >> [1] >> #undef >> > >