# How does AbstractFloat\[1.0f0\] work?

**URL:** <https://discourse.julialang.org/t/how-does-abstractfloat-1-0f0-work/53333>\
**Category:** New to Julia\
**Created:** [January 14, 2021, 12:49pm UTC](https://discourse.julialang.org/t/how-does-abstractfloat-1-0f0-work/53333 "2021-01-14T12:49:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [January 14, 2021, 12:49pm UTC](https://discourse.julialang.org/t/how-does-abstractfloat-1-0f0-work/53333/1 "2021-01-14T12:49:12Z")

</div>

In the blog post [Tutorial on precompilation](https://julialang.org/blog/2021/01/precompile_tutorial/) there is a line `cabs = AbstractFloat[1.0f0] `. While trying to understand the syntax I found one case in the documentation, in the section [Conversion](https://docs.julialang.org/en/v1.7-dev/manual/conversion-and-promotion/#Conversion) there is `a = Any[1 2 3; 4 5 6]`.

What is this syntax? Does it have anything to do with abstract types?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 14, 2021, 1:03pm UTC](https://discourse.julialang.org/t/how-does-abstractfloat-1-0f0-work/53333/2 "2021-01-14T13:03:54Z")

</div>

> [@HenrikM](#):
>
> `cabs = AbstractFloat[1.0f0]`

In this case you are initializing a vector that can contain any type of Floats:

```julia
julia> x = AbstractFloat[1.f0]
1-element Array{AbstractFloat,1}:
 1.0f0

julia> push!(x,1.e0)
2-element Array{AbstractFloat,1}:
 1.0f0 # Float32
 1.0 # Float64

```

If you start a vector with a concrete type signature, like:

```julia
julia> y = Float32[1.f0]
1-element Array{Float32,1}:
 1.0

julia> push!(y,1.e0)
2-element Array{Float32,1}:
 1.0
 1.0

julia> typeof(y[1])
Float32

julia> typeof(y[2])
Float32

```

Only elements of that type will be possible. Depending on what you want the vector to have, you can even get an error:

```julia
julia> z = Int[1]
1-element Array{Int64,1}:
 1

julia> push!(z,sqrt(2))
ERROR: InexactError: Int64(1.4142135623730951)

```

`AbstractFloat` is an abstract type of which `Float32` and `Float64` are subtypes:

```julia
julia> subtypes(AbstractFloat)
4-element Array{Any,1}:
 BigFloat
 Float16
 Float32
 Float64

```

so an `AbstractFloat[]` vector can contain any of those types.

It is not a good a idea to have that kind of mixed type vector, that have performance issues, because each type of number has a different representation in the memory and has to be dispatched differently to the functions that will operate on them.

And `Any[]` can contain anything:

```julia
julia> x = Any[1]
1-element Array{Any,1}:
 1

julia> push!(x,"abc")
2-element Array{Any,1}:
 1
  "abc"

```

---

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [January 14, 2021, 1:15pm UTC](https://discourse.julialang.org/t/how-does-abstractfloat-1-0f0-work/53333/3 "2021-01-14T13:15:39Z")

</div>

> [@lmiq](#):
>
> If you start a vector with a concrete type signature, like:

Ok, thank you! Where is this documented?

---

<div class="post-metadata">

**Author:** ![moeddel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moeddel/32/18641_2.png) [@moeddel](https://discourse.julialang.org/u/moeddel)\
**Post date:** [January 14, 2021, 1:24pm UTC](https://discourse.julialang.org/t/how-does-abstractfloat-1-0f0-work/53333/4 "2021-01-14T13:24:42Z")

</div>

There is a section in the julia documentation under performance tips named [Avoid containers with abstract type parameters](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-abstract-container), which could be of help. In general, you should avoid using abstract types for arrays.

---

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [January 14, 2021, 2:05pm UTC](https://discourse.julialang.org/t/how-does-abstractfloat-1-0f0-work/53333/5 "2021-01-14T14:05:24Z")

</div>

Ok, thank you! This question is not about the performance of abstract types, it is about the syntax in the examples.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 14, 2021, 2:44pm UTC](https://discourse.julialang.org/t/how-does-abstractfloat-1-0f0-work/53333/6 "2021-01-14T14:44:48Z")

</div>

> [@HenrikM](#):
>
> This question is not about the performance of abstract types, it is about the syntax in the examples.

The syntax is explained here, under “Typed Array Literals”: [Single- and multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#Typed-array-literals)
