# Alternative proposal to getindex pun for typed array literals

**URL:** <https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081>\
**Category:** Internals & Design\
**Created:** [December 21, 2016, 12:11am UTC](https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081 "2016-12-21T00:11:17Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [December 21, 2016, 12:11am UTC](https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081/1 "2016-12-21T00:11:18Z")

</div>

I can’t remember where this was discussed, but I do recall someone stating that `Int32[1, 2, 3]` was the one of the “worst puns” in the Julia language (being `getindex` on a type). But now with the “dot-call” syntax, could we get

```julia
Int32.([1,2,3])

```

lowered to

```julia
Base.vect(Int32(1), Int32(2), Int32(3))

```

and similarly for the `vcat`, `hcat` and `hvcat` cases? (the broadcast would need to nest in those cases, I suppose).

I know its a bit more typing but the meaning is clearer, IMO.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 21, 2016, 1:25am UTC](https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081/2 "2016-12-21T01:25:13Z")

</div>

And how do you handle empty arrays? `T[]` was an easy thing to `push!` into. I guess `Vector{T}()` is fine.

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [December 21, 2016, 1:39am UTC](https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081/3 "2016-12-21T01:39:11Z")

</div>

Right, so have the lowering of `T.([])` go to `Vector{T}()`?

---

<div class="post-metadata">

**Author:** ![cdsousa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdsousa/32/215553_2.png) [@cdsousa](https://discourse.julialang.org/u/cdsousa)\
**Post date:** [December 21, 2016, 1:59am UTC](https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081/4 "2016-12-21T01:59:13Z")

</div>

Both `Int32.([1,2,3])` and `Int32.([])` already work, are you proposing an optimized code generation for those cases?

BTW, the syntax `Int.[1,2,3]` seems to be free, maybe it could be useful for something…

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [December 21, 2016, 2:15am UTC](https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081/5 "2016-12-21T02:15:09Z")

</div>

Yes, they already work, and yes I am suggesting optimal code generation via lowering, but I am really suggesting to deprecate `Int[1,2,3]` entirely.

There are already two competing meanings for `Int.[1,2,3]`. One being a broadcasted getindex (it makes sense if you replace `Int` with an array of arrays), or another involving a new interesting indexing scheme which broadcasts the indices themselves (see [https://github.com/JuliaLang/julia/issues/2591](https://github.com/JuliaLang/julia/issues/2591)).

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 21, 2016, 3:34am UTC](https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081/6 "2016-12-21T03:34:27Z")

</div>

> [@andyferris](#):
>
> and yes I am suggesting optimal code generation via lowering

Now do you change lowering?

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [December 21, 2016, 4:55am UTC](https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081/7 "2016-12-21T04:55:14Z")

</div>

> Now do you change lowering?

Sorry, @yuyichao, I don’t quite understand what your question is asking.

So far, I am only making proposals. If people are receptive to this idea, _maybe_ I would have time to make some changes to lowering and a PR, but really I have zero experience in lisp or the Julia’s lowering codebase.

Either way, to implement this for the standard (typed vector: `Float64[1,2,3]`) notation we would (a) add deprecation warnings to `getindex{T}{::Type{T}, X...)` in Base and (b) have the broadcast performed on literal vectors explicitly by the parsing/lowering steps so that we don’t first construct `[1,2,3]` at run-time and then perform `broadcast(Float64, [1,2,3])`. Step (b) is just an optimization to avoid regressions.

Similar steps would have to be done for `Float64[1; 2; 3]`, `Float64[1 2 3]` and `Float64[1 2; 3 4]` (so the user syntax becomes `Float64.([1; 2; 3])`, etc).

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 21, 2016, 5:32am UTC](https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081/8 "2016-12-21T05:32:35Z")

</div>

I mean are you going to change the meaning of `f.([a, b, c])`?

---

<div class="post-metadata">

**Author:** ![martinholters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martinholters/32/3690_2.png) [@martinholters](https://discourse.julialang.org/u/martinholters)\
**Post date:** [December 21, 2016, 1:29pm UTC](https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081/9 "2016-12-21T13:29:10Z")

</div>

One might argue that if

```nohighlight
f.([a, b, c]) == broadcast(f, [a, b, c]) == [f(a), f(b), f(c)]

```

does not hold, something is broken anyway, hence this change of lowering might be an acceptable optimization.

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [December 21, 2016, 2:01pm UTC](https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081/10 "2016-12-21T14:01:09Z")

</div>

> I mean are you going to change the meaning of f.([a, b, c])?

I think the meaning will be preserved (as @martinholters points out) , but the lowering will be different.

EDIT: and yes, `f` doesn’t have to be a type nor does `a`, `b`, `c` have to be literal values - the transformation should hold in general.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 21, 2016, 2:42pm UTC](https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081/11 "2016-12-21T14:42:26Z")

</div>

That argument does not apply to splatting and concatenation.

---

<div class="post-metadata">

**Author:** ![martinholters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martinholters/32/3690_2.png) [@martinholters](https://discourse.julialang.org/u/martinholters)\
**Post date:** [December 21, 2016, 3:15pm UTC](https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081/12 "2016-12-21T15:15:06Z")

</div>

True, splatting couldn’t be handled this way.

Could you expand what you mean by concatenation here?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 21, 2016, 4:07pm UTC](https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081/13 "2016-12-21T16:07:11Z")

</div>

> [@martinholters](#):
>
> Could you expand what you mean by concatenation here?

I mean

> [@andyferris](#):
>
> Similar steps would have to be done for Float64[1; 2; 3], Float64[1 2 3] and Float64[1 2; 3 4] (so the user syntax becomes Float64.([1; 2; 3]), etc).

The these are not simple array construction so it’s less clear if this translation is always correct. (i.e. you can’t easily lower `f.([1; 2; 3])` to `typed_vcat(f, 1, 2, 3)`)

Similarly

> [@andyferris](#):
>
> Right, so have the lowering of T.() go to Vector{T}()?

is problematic.

Finally, now that I’m more wake up… the argument doesn’t hold in general.

`[f(a), f(b), f(c)]` isn’t the correct thing to compare.

Examples of problematic cases includes

```julia
julia> Integer.([1])
1-element Array{Int64,1}:
 1

julia> Any.([1])
1-element Array{Int64,1}:
 1

julia> Integer[1]
1-element Array{Integer,1}:
 1

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

```

* * *

In general, I don’t really find using `getindex` for typed array construction problematic. For syntax, it’s consistent with other array construction/concatenation related syntax. For the implementation, it’s generic (i.e. doesn’t have the issue of only have vectorized version for a subset of functions) and doesn’t conflict with anything else (especially after `Tuple` type is not a `Tuple` anymore) so I don’t see a need to change it.

---

<div class="post-metadata">

**Author:** ![martinholters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martinholters/32/3690_2.png) [@martinholters](https://discourse.julialang.org/u/martinholters)\
**Post date:** [December 21, 2016, 4:13pm UTC](https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081/14 "2016-12-21T16:13:02Z")

</div>

Oh, yes, of course, I stand corrected.

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [December 21, 2016, 10:15pm UTC](https://discourse.julialang.org/t/alternative-proposal-to-getindex-pun-for-typed-array-literals/1081/15 "2016-12-21T22:15:09Z")

</div>

I agree, the syntax does not work as currently for abstract types. My style has always been to use `Vector{T}()` or `Vector{T}(N)` when I care about `T` anyway…

> The these are not simple array construction so it’s less clear if this translation is always correct. (i.e. you can’t easily lower `f.([1; 2; 3])` to `typed_vcat(f, 1, 2, 3))`

For `vcat`, `hcat` and `hvcat`, as I said earlier, the broadcast would have to be recursive: e.g. `T[a; b; c]` would be lowered to `vcat(broadcast(T, a), broadcast(T,b), broadcast(T,c))` (or similar). I suppose that this would involve extra copies… but AFAICT it should be possible to maintain the current behavior.

BTW I’m not convinced _at all_ that this is a good idea - I was just thinking about it after thinking about my own pun on the pun of getindex on types [here](https://github.com/JuliaArrays/StaticArrays.jl/issues/24#issuecomment-268159685) and was curious what the future of this syntax might be.
