# Allow syntax like Int32(1:10)?

**URL:** <https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905>\
**Category:** Internals & Design\
**Created:** [September 18, 2019, 5:41pm UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905 "2019-09-18T17:41:52Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [September 18, 2019, 5:41pm UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/1 "2019-09-18T17:41:52Z")

</div>

basically,

```julia
Int32(a::UnitRange{T}) = Int32(a.start):Int32(a.stop)

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [September 18, 2019, 5:51pm UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/2 "2019-09-18T17:51:19Z")

</div>

It would be pretty weird if `Int32(..)` did not return an `Int32`.

---

<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:** [September 18, 2019, 5:55pm UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/3 "2019-09-18T17:55:06Z")

</div>

`Int32.(1:10)` is the natural syntax for this sort of thing in Julia.

Unfortunately, `Int32.(1:10)` currently gives a dense `Array` object, but it seems like a straightforward optimization to update it to give a `Range`. For example, if we define

```julia
import Base.Broadcast: DefaultArrayStyle, broadcasted

broadcasted(::DefaultArrayStyle{1}, ::Type{T}, r::AbstractUnitRange) where {T<:Integer} = range(T(first(r)), length=T(length(r)))

```

then we would get:

```julia
julia> Int32.(1:10)
1:10

julia> typeof(ans)
UnitRange{Int32}

julia> BigInt.(1:10)
1:10

julia> typeof(ans)
UnitRange{BigInt}

```

Might make a good first PR if someone wanted to take this on.

---

<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:** [September 18, 2019, 5:55pm UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/4 "2019-09-18T17:55:46Z")

</div>

ah, I think this makes sense. Would this make a breaking change though?

---

<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:** [September 18, 2019, 5:59pm UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/5 "2019-09-18T17:59:00Z")

</div>

> [@jling](#):
>
> Would this make a breaking change though?

A range is still an `AbstractArray` type, and we don’t generally guarantee what type of array a `broadcast` will produce, so I think this would qualify as a “minor change” that could go into (e.g.) Julia 1.4. This would be discussed in a PR.

---

<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:** [September 18, 2019, 8:22pm UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/6 "2019-09-18T20:22:53Z")

</div>

But people can reasonably rely on the return value of something being mutatable.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 19, 2019, 4:28am UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/7 "2019-09-19T04:28:43Z")

</div>

Just to add for completeness

```julia
Vector{Int32}(1:10)

```

works too

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [September 19, 2019, 4:40am UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/8 "2019-09-19T04:40:23Z")

</div>

Favor `convert` rather than constructors,

```julia
convert(UnitRange{Int32}, 1:10)

```

---

<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:** [September 19, 2019, 5:28am UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/9 "2019-09-19T05:28:07Z")

</div>

You’re stating that like a general rule. What’s the argument? I certainly favor constructors over the very verbose constructor syntax.

Especially in cases where the point is to quickly and conveniently instantiate something at the REPL.

---

<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:** [September 19, 2019, 7:45am UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/10 "2019-09-19T07:45:34Z")

</div>

I’d rather `Int32(1):Int32(10)` in that case.

---

<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:** [September 19, 2019, 7:46am UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/11 "2019-09-19T07:46:12Z")

</div>

No, this materialize the iterator thus break the purpose.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [September 19, 2019, 7:52am UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/12 "2019-09-19T07:52:11Z")

</div>

> [@DNF](#):
>
> You’re stating that like a general rule. What’s the argument? I certainly favor constructors over the very verbose constructor syntax.

The fundamental difference between constructors and `convert` is that the former makes a copy but the latter doesn’t when possible. Here it doesn’t matter since we know the returned object will be of a different type from the input, so a new object will have to be returned (and `UnitRange` is immutable anyway).

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [September 19, 2019, 8:35pm UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/13 "2019-09-19T20:35:35Z")

</div>

> [@yuyichao](#):
>
> But people can reasonably rely on the return value of something being mutatable.

If you need mutability you should probably be safe and use

```Julia
convert(Vector, Int32.(1:9))

```

This will work in Julia v1.0-1.4.

---

<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:** [September 19, 2019, 8:48pm UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/14 "2019-09-19T20:48:09Z")

</div>

I know the correct way to do it if the change is made, but that doesn’t mean the change is non breaking.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [September 19, 2019, 8:58pm UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/15 "2019-09-19T20:58:45Z")

</div>

Bug fixes are also breaking changes… I think this is arguably on the level of a bug fix.

---

<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:** [September 19, 2019, 9:18pm UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/16 "2019-09-19T21:18:27Z")

</div>

No this is definitely not a bug fix. It returns a result that makes sense and you are changing it.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [September 19, 2019, 9:52pm UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/17 "2019-09-19T21:52:56Z")

</div>

It’s using undocumented behaviour. The fact that `UInt32.(1:10)` returns a `Vector` is simply an implementation detail, and it’s a users fault for mutating it when there is no a priori guarantee that it’s mutable.

---

<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:** [September 19, 2019, 10:13pm UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/18 "2019-09-19T22:13:33Z")

</div>

No, that doesn’t make any sense. It’s at most missing document (and it’s not really missing either, see below). Undocumented doesn’t mean it’s implementation detail (and FWIW implementation detail could be documented as well).

It would have made sense if all other functions and syntaxes are documented to this level (what are the exact input types, what are the output types you expect, what are the output types and values under each conditions, etc).

Your argument is just that the user should never rely on anything they see, which means that they should never try type something into the REPL to see if it works since that has absolutely zero meaning. I would actually think that’s a good thing if the documentation is better (too many GC related bugs in user code from this). This would have been the case if you are talking about a specification. The documentation is not good enough to be a specification (yet) so that reasoning is wrong.

As another example, [this](https://docs.julialang.org/en/v1/base/math/#Base.cos-Tuple%7BNumber%7D) is the documentation for `cos`.

> Compute cosine of `x` , where `x` is in radians.

From your logic, it’s the user’s fault if he assume that `cos(1)::Float64` (or even `cos(1)` is convertible to `Float64`). In fact, it’s even his fault to assume the return value is usable at all, the document doesn’t mention returning. It could compute it and return some undefined number and it’ll totally be inline with the document. It could return an `RefValue` or an `Vector`/`Array` containing the result just like matlab and that totally satisfies the document. These are things that must be specified in a specification but is also reasoable to left undocumented otherwise since the behavior is easy to observe and can easily be reasoned about.

And again, just to get the logic as clear as possible, I’m not saying **NONE of** the undocumented behaviors are implementation detail. I’m not even saying that all of the observable effects are dependable. However, it’s clearly wrong to say that undocumented implies implementation detail.

Also, in this specific case, it is understood that broadcast returns array by default. Any deviation from such a convention needs documentation.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [September 20, 2019, 6:18am UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/19 "2019-09-20T06:18:40Z")

</div>

For the case of a literal `1:10` (as opposed to `a:b` where `a` and `b` happen to be `1` and `10`), one could handle this with a macro. Something like:

```julia
i32(x::Int64) = typemin(Int32) <= x <= typemax(Int32) ? Int32(x) : x
i32(x::Expr) = Expr(x.head, i32.(x.args)...)
i32(x) = x
macro i32(x)
    i32(x)
end

```

Now `@i32 1:10` will return a `UnitRange{Int32}`. The macro can also be used on larger blocks of code, for example

```julia
@i32 let s = 0
    for i = 1:10
         s += i
     end
     s
end

```

will return an Int32

This type of macro can be useful when targeting the GPU or embedded processors where it’s otherwise easy to accidentally introduce promotion to 64-bit.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 20, 2019, 7:01am UTC](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905/20 "2019-09-20T07:01:18Z")

</div>

> [@Per](#):
>
> targeting the GPU or embedded processors where it’s otherwise easy to accidentally introduce promotion to 64-bit.

Wouldn’t it be better to error then for values that cannot be converted?

Also, I was under the impression that GPU interfaces sanitize their input, and then keep everything in the valid type set.

[Next page](https://discourse.julialang.org/t/allow-syntax-like-int32-1-10/28905.md?page=2)
