``Any[1, 1.0]`` is valid code, but I can't find why in the docs

I love the fact that I can decorate [1.0, 1] as Any[1, 1.0] and Real[1, 1.0] and have mixed number type as elements.

But I can’t find where in the docs this is described, and I am not even sure where in the docs it should go—is it part of the discussion of types, or of arrays, or both?

I expected decoration to work also for user-defined types, and it does.

Looking for


Indeed! Thanks!

Note however, that operations on Arrays like these can be really slow because of type instability and you’re usually much better off using a Tuple or a different data structure.

julia> DuckTyping = Any

julia> DuckTyping[3,4.5,"tiger",big(6),big(77.777)]
5-element Array{Any,1}:

Oh, agreed! I stumbled across this because I am writing a course for Julia as a first programming language, and it raises lots of flags for me.
T[...] looks indexing
— but is in fact a constructor, which is a function, which should have round brackets, as far as beginners are concerned
— and in any case I want to say as little as possible about the type system

I’m trying to follow the slogan “Small steps, leave nothing out, everything makes sense” so that rank beginners who are easily put off will find that they can follow along without missing anything. Of course that is not suitable for the hot-shots who become professional programmers, but I believe programming should be like singing and dancing: everybody should be able to do it in at least a small way.

It actually is:

julia> getindex(Float64, 1, 2, 3)
3-element Array{Float64,1}:

see https://github.com/JuliaLang/julia/blob/efd794e199994237e1278a69b38ddad9f949b6b5/base/array.jl#L321-L363

Possibly (and as noted above by @fredrikekre , also in terms of the implementation), but it can also be considered an extension of the vector constructor [1, 2, ...]. []s are used for other purposes, too, eg comprehensions.

Many languages use building blocks of syntax for multiple purposes, especially if it does not create an ambiguity.

In Julia it is very easy to stumble upon advanced concepts while evaluating seemingly simple code. Beginners in Julia, even if they are experienced programmers in other languages, tend to miss a lot initially, just because the language is quite different from what they may be used to. The trick is not to mind missing things initially, and then get them in a second, or third pass.

I’d agree, for motivated and relaxed learners. My course has the tentative title “Julia Programming for Nervous Beginners” and I am trying to find a balance where advanced topics, when they come up, can be safely broken into “this is the bit that concerns you” and “the rest” without in any way generating anxiety in the learners that may be prone to that.

So far, the type system is where I find it the hardest to design a sequence that is somewhat satisfying. My plan is to do functions very early (before for loops, for example), introduce multiple dispatch by an example, and attempt to discuss the type system only in terms of helping with multiple dispatch (ignoring compiler optimisation in particular).

1 Like

This sounds like a reasonable plan. While I think that 90% of Julia programming is about “cooperating with the compiler”, I agree that this may very confusing for beginners (sometimes it is also challenging for seasoned users).