# Units, errors and type display

**URL:** https://discourse.julialang.org/t/units-errors-and-type-display/11220
**Category:** General Usage
**Tags:** question
**Created:** [May 29, 2018, 7:10am UTC](https://discourse.julialang.org/t/units-errors-and-type-display/11220 "2018-05-29T07:10:02Z")
**Posts on this page:** 9
**Page:** 2

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [May 30, 2018, 1:23am UTC](https://discourse.julialang.org/t/units-errors-and-type-display/11220/21 "2018-05-30T01:23:54Z")

</div>

Do show methods change the types in error output? I couldn’t get that to work, I don’t really understand how it works

---

<div class="post-metadata">

### Author: ![phlavenk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phlavenk/32/1873_2.png) [@phlavenk](https://discourse.julialang.org/u/phlavenk)
#### Post date: [May 30, 2018, 6:16am UTC](https://discourse.julialang.org/t/units-errors-and-type-display/11220/22 "2018-05-30T06:16:41Z")

</div>

I’m also a big fan of Unitful. I thing units should get a first class treatment (may be as a part of stdlib) in the Julia ecosystem.  
Nowadays using units e.g. for both values and axes of AxisArrays.jl ends up producing a pretty complex type output.  
Just think of the readability of a simple StepRange from 1 to 100 seconds.

```julia
using Unitful
typeof(1u"s" : 1u"s" : 100u"s")

StepRange{Unitful.Quantity{Int64,Unitful.Dimensions{(Unitful.Dimension{:Time}
(1//1),)},Unitful.FreeUnits{(Unitful.Unit{:Second,Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}}(0,
1//1),),Unitful.Dimensions{(Unitful.Dimension{:Time}
(1//1),)}}},Unitful.Quantity{Int64,Unitful.Dimensions{(Unitful.Dimension{:Time}
(1//1),)},Unitful.FreeUnits{(Unitful.Unit{:Second,Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}}(0, 
1//1),),Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}}}}

```

We definitively need a way for introducing a type alias for error outputs or printing human readable type info. In my code I define many `const ` statements like this

```julia
const QAxis{Name, Dim} = Axis{Name, AX} where AX <: AbstractArray{Q} where Q <: Dim
const TimeAxis = QAxis{S, Unitful.Time} where S;
const TimeArray{T, N, V} = AxisArray{T, N, V, <:Tuple{TimeAxis, Vararg{Axis}}};

```

and I would be very happy if I could see similar simplified view on the REPL error messages and stacktraces.

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [May 30, 2018, 6:47am UTC](https://discourse.julialang.org/t/units-errors-and-type-display/11220/23 "2018-05-30T06:47:11Z")

</div>

Yeah Unitful is pretty central for me too. There’s an issue tracking this now:

[https://github.com/JuliaLang/julia/issues/27305](https://github.com/JuliaLang/julia/issues/27305)

---

<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 30, 2018, 7:16am UTC](https://discourse.julialang.org/t/units-errors-and-type-display/11220/24 "2018-05-30T07:16:01Z")

</div>

> [@phlavenk](#):
>
> ```julia
> using Unitful typeof(1u"s" : 1u"s" : 100u"s") 
> StepRange{Unitful.Quantity{Int64,Unitful.Dimensions{(Unitful.Dimension{:Time} 
> (1//1),)},Unitful.FreeUnits{(Unitful.Unit{:Second,Unitful.Dimensions{(Unitful.Dimension{:ticket: me}(1//1),)}}(0, 1//1),),Unitful.Dimensions{(Unitful.Dimension{:Time} 
> (1//1),)}}},Unitful.Quantity{Int64,Unitful.Dimensions{(Unitful.Dimension{:Time} 
> (1//1),)},Unitful.FreeUnits{(Unitful.Unit{:Second,Unitful.Dimensions{(Unitful.Dimension{:ticket: me}(1//1),)}}(0, 1//1),),Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}}}}
> 
> ```

Wow, that’s big. I thought it had something to do with the interplay between `StepRange` and units, but even this is gigantic:

```julia
julia> typeof(1u"s")
Unitful.Quantity{Int64,Unitful.Dimensions{(Unitful.Dimension{:Time}
(1//1),)},Unitful.FreeUnits{(Unitful.Unit{:Second,Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}}(0, 
1//1),),Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}}}

```

This must be the simplest thing you can define with Unitful, and yet it has a type signature like that. I would have naively expected something like this:

```julia
Unitful.Quantity{Int64, Unitful.Dimension{:Time}, Unitful.Unit{:Second}}

```

**Edit:** Actually, I was sort of expecting

```julia
Unitful.Quantity{Int64, Unitful.Unit{:Second}}

```

so this is clearly a lot more complex than I imagined.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [May 30, 2018, 7:44am UTC](https://discourse.julialang.org/t/units-errors-and-type-display/11220/25 "2018-05-30T07:44:21Z")

</div>

One of the problems with type parameters in Julia is that they can be both part of the (public) API of a type, but also just implementation detail. Say for `Array{Int,1}`, I’d say that both parameters are part of the public API, so it is good to see them. However, for other types, e.g. this struct holding a bunch of parameters:

```julia
struct Foo{F<:Function}
  ...
  fn:F 
end

```

Here the `F` is not part of the API (the type parameter is only needed for performance). Of course there are gray areas too.

So, somehow separating the public vs non-public and prioritizing the printing of the public ones could also help with this issue.

---

<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 30, 2018, 7:48am UTC](https://discourse.julialang.org/t/units-errors-and-type-display/11220/26 "2018-05-30T07:48:16Z")

</div>

That’s an interesting point. When typing

```julia
typeof(1u"s":1u"s":100u"s")

```

I think that this is what I’d like to see:

```julia
StepRange{Unitful.Quantity{Int64, :Second}}

```

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [May 30, 2018, 8:08am UTC](https://discourse.julialang.org/t/units-errors-and-type-display/11220/27 "2018-05-30T08:08:18Z")

</div>

That looks great. You could strip out the `Unitful.`

`StepRange{Quantity{Int64, :Second}}`

But I think having :Second would require a customised type printing mechanism, as there’s no sensible way of choosing between :Time, :Second, Dimensions, FreeUnits etc…

---

<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 30, 2018, 8:37am UTC](https://discourse.julialang.org/t/units-errors-and-type-display/11220/28 "2018-05-30T08:37:19Z")

</div>

> [@Raf](#):
>
> But I think having :Second would require a customised type printing mechanism, as there’s no sensible way of choosing between :Time, :Second, Dimensions, FreeUnits etc…

Yes, this was not thought through, and isn’t a serious proposition. Just firing from the hip. I expect Unitful does a lot of extra stuff that requires more complexity.

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [May 30, 2018, 9:47am UTC](https://discourse.julialang.org/t/units-errors-and-type-display/11220/29 "2018-05-30T09:47:42Z")

</div>

> [@ChrisRackauckas](#):
>
> But yes, there must be some easier way to parse this. I really like the Juno tree displays and would like to see them as part of the Base display system, but of course they wouldn’t be usable from the REPL (I think?)

This should actually be quite simple to get working in the REPL as well, afaict. I’m prototyping something based on [TerminalMenus.jl](https://github.com/nick-paul/TerminalMenus.jl) right now, which is part of the `REPL` stdlib anyways.

Sneak peak:  
 ![terminalmenus](https://global.discourse-cdn.com/julialang/original/3X/2/5/253538c326097a1ff69bf1489fdaa1903c1563ab.gif)

Still a couple of things to figure out, but seems like a good idea overall imho.

[Previous page](https://discourse.julialang.org/t/units-errors-and-type-display/11220.md?page=1)
