Preference for Array{T,1} over Vector{T}?

244 views
Skip to first unread message

Scott Jones

unread,
May 13, 2015, 1:46:19 PM5/13/15
to juli...@googlegroups.com
I've noticed in the code (and on Jeff's slides) that people almost always use Array{T,1} instead of Vector{T} (which is just an alias, but I think it's more readable...)
Is there any particular reason for that?

Isaiah Norton

unread,
May 14, 2015, 10:37:45 AM5/14/15
to juli...@googlegroups.com
It is really just a matter of taste...
(this sort of question should go to julia-users)

Scott Jones

unread,
May 14, 2015, 10:40:19 AM5/14/15
to juli...@googlegroups.com
I asked here, because I wondered if this was just historical or not (i.e. if the Vector alias was just added recently).

I also saw something in the base code that I wasn’t sure was correct, it used AbstractArray{UInt16} instead of AbstractArray{UInt16,1} for a UTF-16 string,
and it looks like those are different somehow…

Marcus Appelros

unread,
May 14, 2015, 10:46:34 AM5/14/15
to juli...@googlegroups.com

Dicussions about about coding conventions in the Julia source seem to fit well in julia-dev.

Kevin Squire

unread,
May 14, 2015, 10:49:57 AM5/14/15
to juli...@googlegroups.com


On Thursday, May 14, 2015, Scott Jones <scott.pa...@gmail.com> wrote:
I asked here, because I wondered if this was just historical or not (i.e. if the Vector alias was just added recently).

The alias has been there for a long time. Being able to call Vector{Int} as a constructor is new
 
I also saw something in the base code that I wasn’t sure was correct, it used AbstractArray{UInt16} instead of AbstractArray{UInt16,1} for a UTF-16 string,
and it looks like those are different somehow…

It depends on where it is used. As a function parameter type, it should work fine, because the compiled version will specialize on the actual type of the vector. (It will also accept N>=2 dimensional UInt16 arrays, which might not make much sense, but probably doesn't hurt anything.)

I would guess most other uses are wrong, but it depends on the actual use. 

Cheers,
   Kevin 

Isaiah Norton

unread,
May 14, 2015, 10:52:45 AM5/14/15
to juli...@googlegroups.com
I'm pretty sure that alias has been available since 0.1 at least.

I also saw something in the base code that I wasn’t sure was correct, it used AbstractArray{UInt16} instead of AbstractArray{UInt16,1} for a UTF-16 string,
and it looks like those are different somehow…

Yes, the former is parameterized over any number of dimensions; the latter only accepts "vectors".

> n(::AbstractArray{Int}) = println("N")
> n([1, 2])
> n([1 2; 3 4])
> n(::AbstractArray{Int,1}) = println("1")
> n([1, 2])
> n([1 2; 3 4])

N
N
1
N


Scott Jones

unread,
May 14, 2015, 12:24:45 PM5/14/15
to juli...@googlegroups.com
I saw it in io.jl:
function readbytes!(s::IO, b::AbstractArray{UInt8}, nb=length(b))


and in utf16.jl and utf32.jl:
function is_valid_utf16(data::AbstractArray{UInt16})convert(T::Type{UTF16String}, data::AbstractArray{UInt16}) =
    convert(T, reshape(data, length(data)))

convert(T::Type{UTF16String}, data::AbstractArray{Int16}) =
    convert(T, reinterpret(UInt16, data))

function convert(T::Type{UTF16String}, bytes::AbstractArray{UInt8})


function convert(T::Type{UTF32String}, bytes::AbstractArray{UInt8})


It seems like a bug to me... strings are only 1 dimensional... shouldn’t all of these be AbstractVector?

Thanks, Scott

Tim Holy

unread,
May 14, 2015, 12:33:00 PM5/14/15
to juli...@googlegroups.com
readbytes! is not just about strings. This way, you can directly read into a
multidimensional array without copying or reshaping.

--Tim

Scott Jones

unread,
May 14, 2015, 1:39:24 PM5/14/15
to juli...@googlegroups.com
Ah, good point... still not used to the way multidimensional arrays work in Julia (I was used to multidimensional associative arrays).
What about the cases in utf16.jl and utf32.jl?  Am I correct that those really should be AbstractVector?

Thanks!

Steven G. Johnson

unread,
May 15, 2015, 6:13:40 PM5/15/15
to juli...@googlegroups.com
On Thursday, May 14, 2015 at 1:39:24 PM UTC-4, Scott Jones wrote:
Ah, good point... still not used to the way multidimensional arrays work in Julia (I was used to multidimensional associative arrays).
What about the cases in utf16.jl and utf32.jl?  Am I correct that those really should be AbstractVector?
 
There is never any performance reason to restrict the argument types, so the only reasons are (a) disambiguation with other methods, (b) correctness [if it would silently give the wrong answer for some types], and/or (c) clarity [as a hint to the user about what makes sense].

In this case, there is no need for disambiguation, since we don't define any separate "multidimensional string" algorithm.   And there's nothing really "incorrect" about taking a multidimensional array of UInt32 and collapsing it into a single string.  This is an odd operation to do, but why not allow it if it requires no additional code (and in fact, fewer characters of code than restricting the type would require)?  The only question is whether it is somehow confusing to allow this.

I don't care too much one way or the other, but my general inclination is to declare the widest argument type for which the function acts in a reasonable way.

Scott Jones

unread,
May 15, 2015, 7:38:13 PM5/15/15
to juli...@googlegroups.com
Great answer!  I hadn't realized that a multidimensional AbstractArray could get "collapsed" into a single string...
Is it then iterable with `for ch in array` or using `start(array)`,`endof(array)` and `nextind(array,pos)`?
Can an abstract array even be something like a tuple of vectors?  Would something like that be flattened into a vector-like "view"?

Unfortunately, from what I can see, the string code is *very* inconsistent then about allowing for `AbstractArray(str)`.
Most all places have things like: `Array{UInt8,1}`

I agree definitely about declaring the widest argument type... I just didn't know that taking a multidimensional array could be acted upon in a reasonable way by string functions.

Cool!
Reply all
Reply to author
Forward
0 new messages