# Array{Float32} literal?

**URL:** <https://discourse.julialang.org/t/array-float32-literal/81195>\
**Category:** New to Julia\
**Created:** [May 17, 2022, 12:43pm UTC](https://discourse.julialang.org/t/array-float32-literal/81195 "2022-05-17T12:43:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [May 17, 2022, 12:43pm UTC](https://discourse.julialang.org/t/array-float32-literal/81195/1 "2022-05-17T12:43:48Z")

</div>

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

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

```

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

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

```

Is there a better way?

I imagine that something like this

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

```

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

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 17, 2022, 12:44pm UTC](https://discourse.julialang.org/t/array-float32-literal/81195/2 "2022-05-17T12:44:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 17, 2022, 1:04pm UTC](https://discourse.julialang.org/t/array-float32-literal/81195/3 "2022-05-17T13:04:50Z")

</div>

```julia
[1f0, 2f0, 3f0, 4f0]

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 17, 2022, 1:14pm UTC](https://discourse.julialang.org/t/array-float32-literal/81195/4 "2022-05-17T13:14:48Z")

</div>

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

By the way, this is better written as

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

```

which is better written as

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

```

which is better written as

```julia
a = Float32[1,2,3,4]

```

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [May 18, 2022, 3:34am UTC](https://discourse.julialang.org/t/array-float32-literal/81195/5 "2022-05-18T03:34:16Z")

</div>

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
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)

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 18, 2022, 3:40am UTC](https://discourse.julialang.org/t/array-float32-literal/81195/6 "2022-05-18T03:40:03Z")

</div>

[https://docs.julialang.org/en/v1/manual/arrays/#Typed-array-literals](https://docs.julialang.org/en/v1/manual/arrays/#Typed-array-literals)

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [May 18, 2022, 4:11am UTC](https://discourse.julialang.org/t/array-float32-literal/81195/7 "2022-05-18T04:11:49Z")

</div>

> [@ryofurue](#):
>
> I imagine that something like this
> 
> ```julia
> a = [1, 2, 3, 4]::Vector{Float32}
> 
> ```
> 
> might be available (similarly to Haskell’s `a = [1,2,3,4]::[Float]` ).

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

```julia
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
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

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 18, 2022, 5:26am UTC](https://discourse.julialang.org/t/array-float32-literal/81195/8 "2022-05-18T05:26:58Z")

</div>

> [@ryofurue](#):
>
> I understand all the answers, except for
> 
> > Float32[1,2,3,4]
> 
> My question is, **what** is this?

```julia
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.

> [@ryofurue](#):
>
> `func[1,2,3]`

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

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [May 18, 2022, 6:32am UTC](https://discourse.julialang.org/t/array-float32-literal/81195/9 "2022-05-18T06:32:40Z")

</div>

> [Single- and multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#Typed-array-literals)

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.

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [May 18, 2022, 6:38am UTC](https://discourse.julialang.org/t/array-float32-literal/81195/10 "2022-05-18T06:38:28Z")

</div>

> `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.

---

<div class="post-metadata">

**Author:** ![yashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yashi/32/19448_2.png) [@yashi](https://discourse.julialang.org/u/yashi)\
**Post date:** [May 19, 2022, 2:11am UTC](https://discourse.julialang.org/t/array-float32-literal/81195/11 "2022-05-19T02:11:02Z")

</div>

> [@ryofurue](#):
>
> A Google search for “Julia array literal”

The first result for me is the [Multi-dimensional Arrays](https://docs.julialang.org/en/v1/manual/arrays/#Typed-array-literals). And the section [Array literals](https://docs.julialang.org/en/v1/manual/arrays/#man-array-literals) is in the page.

Problems for me are that

- two subsections of it, [Concatenation](https://docs.julialang.org/en/v1/manual/arrays/#man-array-concatenation) and [Typed array literals](https://docs.julialang.org/en/v1/manual/arrays/#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?
