# Array{Float32} literal?

We can get a `Vector{Float32}` literal with

``````a = [1.0f0, 2.0f0, 3.0f0, 4.0f0]
``````

but it’s a bit tedious. I can think of

``````a = map(i->Float32(i), [1,2,3,4])
``````

Is there a better way?

I imagine that something like this

``````a = [1, 2, 3, 4]::Vector{Float32}
``````

might be available (similarly to Haskell’s `a = [1,2,3,4]::[Float]`).

`Float32[1,2,3,4]`

14 Likes
``````[1f0, 2f0, 3f0, 4f0]
``````
1 Like

By the way, this is better written as

``````a = map(Float32, [1,2,3,4])  # no need for making an anonymous function
``````

which is better written as

``````a = Float32.([1,2,3,4])
``````

which is better written as

``````a = Float32[1,2,3,4]
``````
8 Likes

Thank you all for your help!

I understand all the answers, except for

Float32[1,2,3,4]

My question is, what is this?

Is the above equivalent to `Float32.([1,2,3,4])`? No. Because

``````julia> func = x -> 3*x
#1 (generic function with 1 method)

julia> func.([1,2,3])
3-element Vector{Int64}:
3
6
9

julia> func[1,2,3]
ERROR: MethodError: no method matching getindex(::var"#1#2", ::Int64, ::Int64, ::Int64)
``````
1 Like
2 Likes

By the way, you can actually do this with local bindings[1]

``````julia> let a::Vector{Float32} = [1,2,3,4]
a
end
4-element Vector{Float32}:
1.0
2.0
3.0
4.0
``````

Though, be aware that this allocates two arrays, one that is `[1,2,3,4]` and a new one from the implicit call to `convert(Vector{Float32}, [1,2,3,4])`. One day Julia May be able to optimize away that allocation, but currently it cannot.

Though I guess you could instead write

``````julia> let a::Vector{Float32} = 1:4
a
end
4-element Vector{Float32}:
1.0
2.0
3.0
4.0
``````

[1] in version 1.8, this will be possible even in the global scope

1 Like
``````T[1,2,3]
``````

makes an array with elements of type `T`. It is better than `T.([1,2,3])`, because the latter will first make a `Vector{Int}`, and then convert it to a `Vector{T}`, while `T[1,2,3]` will directly create the wanted array type, no extra allocation.

This doesn’t work since `func` isn’t a type (or, technically, because it has no `getindex` method.)

1 Like

Multi-dimensional Arrays · The Julia Language

Thanks! That clearly shows that I should search the official Julia documentation rather than Google. A Google search for “Julia array literal” didn’t (quickly enough) lead me to this answer.

Reading the other answers (Thank you all!), I now understand that `T[1,2,3,4]` really is the syntax for array literal.

I’ve also learned that the type annotation works only on variables, not on literals.

2 Likes

`a::Vector{Float32} = [1,2,3,4]`
. . . . . . . . . . .
[1] in version 1.8, this will be possible even in the global scope

Ah, that was what the error message meant, when I first tried that on my global variable before posting my initial question here!

Anyway, I understand your explanation that an allocation happens on the right-hand side.

1 Like

The first result for me is the Multi-dimensional Arrays. And the section Array literals is in the page.

Problems for me are that

• two subsections of it, Concatenation and Typed array literals aren’t listed in the section list in the left pane, and
• the Concatenation section is rather long and doesn’t show the Typed array literals section at glance.

Would it be better to either

• swap the order of the sections in the doc, or
• show subsections under selected section in the navigator?
2 Likes