# Initializing array of struct with literals

**URL:** https://discourse.julialang.org/t/initializing-array-of-struct-with-literals/43073
**Category:** General Usage
**Created:** [July 14, 2020, 6:15pm UTC](https://discourse.julialang.org/t/initializing-array-of-struct-with-literals/43073 "2020-07-14T18:15:23Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [July 14, 2020, 6:15pm UTC](https://discourse.julialang.org/t/initializing-array-of-struct-with-literals/43073/1 "2020-07-14T18:15:23Z")

</div>

I have:

```julia
using Luxor
pl = [Point(0,0), Point(1,1), Point(2,0), Point(3,0), Point(4,0)]

```

where `Luxor.Point` is defined as:

```julia
struct Point
   x::Float64
   y::Float64
end

```

Is there a way to initialize `pl` without repeating the constructor call for each element? Something along the lines of:

```julia
pl = Array{Point, 1}[(0,0), (1,1), (2,0), (3,0), (4,0)]

```

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [July 14, 2020, 6:23pm UTC](https://discourse.julialang.org/t/initializing-array-of-struct-with-literals/43073/2 "2020-07-14T18:23:35Z")

</div>

Yes, use an array comprehension:

```julia
pl = [Point(i, j) for (i, j) in ((0,0), (1, 1), (2, 0), (3, 0), (4, 0))]

```

---

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [July 14, 2020, 6:34pm UTC](https://discourse.julialang.org/t/initializing-array-of-struct-with-literals/43073/3 "2020-07-14T18:34:19Z")

</div>

> [@Henrique\_Becker](#):
>
> `pl = [Point(i, j) for (i, j) in ((0,0), (1, 1), (2, 0), (3, 0), (4, 0))]`

Thank you. This is much less tedious.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [July 14, 2020, 7:39pm UTC](https://discourse.julialang.org/t/initializing-array-of-struct-with-literals/43073/4 "2020-07-14T19:39:38Z")

</div>

Does the `Luxor.Point` constructor allow for taking in a `Tuple`? If so you could just do

```julia
Point.(points)

```

where `points` is a vector of tuples each of length `2`.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [July 14, 2020, 7:57pm UTC](https://discourse.julialang.org/t/initializing-array-of-struct-with-literals/43073/5 "2020-07-14T19:57:06Z")

</div>

Otherwise, this will also work:

```julia
((t,) -> Point(t[1], t[2])).([(0,0), (1, 1), (2, 0), (3, 0), (4, 0)])

```

But it remembers me of:

![](https://global.discourse-cdn.com/julialang/original/3X/5/c/5cfbdcba4a5230fb18c2d06a3009e102fbf1caa8.png)

---

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [July 15, 2020, 2:30am UTC](https://discourse.julialang.org/t/initializing-array-of-struct-with-literals/43073/6 "2020-07-15T02:30:11Z")

</div>

> [@pdeffebach](#):
>
> Does the `Luxor.Point` constructor allow for taking in a `Tuple` ?

No, it doesn’t:

```julia
julia> pl = Point.((0,0), (1,1), (2,0), (3,0), (4,0))
ERROR: MethodError: no method matching Point(::Int64, ::Int64, ::Int64, ::Int64, ::Int64)

```

It also does something quite unexpected for me:

```julia
julia> pl = Point.((0,0), (1,1))
(Point(0.0, 1.0), Point(0.0, 1.0))

```

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [July 15, 2020, 2:35am UTC](https://discourse.julialang.org/t/initializing-array-of-struct-with-literals/43073/7 "2020-07-15T02:35:26Z")

</div>

That’s just broadcasting; you could get what you want with it: `Point.(0:4, 0)`

---

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [July 15, 2020, 6:00am UTC](https://discourse.julialang.org/t/initializing-array-of-struct-with-literals/43073/8 "2020-07-15T06:00:09Z")

</div>

I defined:

```julia
Point((x, y)::Tuple{Real, Real}) = Point(x, y)

```

and now I can write:

```julia
pl = Point.([(0, 0), (1, 1), (2, 0), (3, 1), (4, 0)])

```
