# Tutorial on using advanced type system in Julia?

**URL:** <https://discourse.julialang.org/t/tutorial-on-using-advanced-type-system-in-julia/32903>\
**Category:** New to Julia\
**Created:** [January 3, 2020, 1:11am UTC](https://discourse.julialang.org/t/tutorial-on-using-advanced-type-system-in-julia/32903 "2020-01-03T01:11:16Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![tro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tro3/32/12355_2.png) [@tro3](https://discourse.julialang.org/u/tro3)\
**Post date:** [January 4, 2020, 6:31pm UTC](https://discourse.julialang.org/t/tutorial-on-using-advanced-type-system-in-julia/32903/21 "2020-01-04T18:31:59Z")

</div>

Success! Ignore the redundant pmap function - just a testcase. The point is that the hack results in the correct inferral. Seems like there should be a more built-in way to do this.

```julia
pmap2(fn, itr) = pmap2(fn, itr, eltype(typeof(map(fn, []))))

function pmap2(fn, itr, ::Type{T}) where T
    results = Vector{T}(undef, length(itr))
...
    return results
end

julia> @code_warntype pmap2(x::Int->x/2,[1,2])
Variables
  #self#::Core.Compiler.Const(pmap2, false)
  fn::Core.Compiler.Const(var"#350#351"(), false)
  itr::Array{Int64,1}

Body::Array{Float64,1}
1 ─ %1 = Base.vect()::Array{Any,1}
│ %2 = Main.map(fn, %1)::Array{Float64,1}
│ %3 = Main.typeof(%2)::Core.Compiler.Const(Array{Float64,1}, false)
│ %4 = Main.eltype(%3)::Core.Compiler.Const(Float64, false)
│ %5 = Main.pmap2(fn, itr, %4)::Array{Float64,1}
└── return %5

```

---

<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:** [January 4, 2020, 6:48pm UTC](https://discourse.julialang.org/t/tutorial-on-using-advanced-type-system-in-julia/32903/22 "2020-01-04T18:48:25Z")

</div>

The built-in way to do it is: `Core.Compiler.return_type(fn, Tuple{Any})` (and this is likely what gets called in the end) but I think `eltype(map(fn, []))` is a nice way of writing it.

(The `typeof` is not necessary.)

---

<div class="post-metadata">

**Author:** ![tro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tro3/32/12355_2.png) [@tro3](https://discourse.julialang.org/u/tro3)\
**Post date:** [January 4, 2020, 6:53pm UTC](https://discourse.julialang.org/t/tutorial-on-using-advanced-type-system-in-julia/32903/23 "2020-01-04T18:53:10Z")

</div>

👍 Perfect - thanks!

---

<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:** [January 4, 2020, 7:02pm UTC](https://discourse.julialang.org/t/tutorial-on-using-advanced-type-system-in-julia/32903/24 "2020-01-04T19:02:34Z")

</div>

A minor remark: You don’t have to specify the argument type in the definition of `fn`. Instead you can specify the element type of the empty array:

```julia
fn = x->x/2
itr = 1:3
eltype(map(fn, eltype(itr)[])) # Float64
itr2 = big.(1:3)
eltype(map(fn, eltype(itr2)[])) # BigFloat

```

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [January 6, 2020, 10:44am UTC](https://discourse.julialang.org/t/tutorial-on-using-advanced-type-system-in-julia/32903/25 "2020-01-06T10:44:49Z")

</div>

Since `first` is type-stable, wouldn’t it be simpler to use something like this?

```julia
typeof(fn(first(itr)))

```

Also, I’m not sure that the two-stage process is needed. For example:

```julia
function pmap3(fn, itr)
    T = typeof(fn(first(itr)))
    results = Vector{T}(undef, length(itr))

    i = 1
    for x in itr
        @inbounds results[i] = fn(x)
        i+=1
    end
    return results
end

```

yields

```julia
julia> using Test

# "simple" case of an Array
julia> @inferred pmap3(x->x/2, [1, 2])
2-element Array{Float64,1}:
 0.5
 1.0

# More complex case with a generator
julia> @inferred pmap3(x->x/2, (1//i for i in 1:2))
2-element Array{Rational{Int64},1}:
 1//2
 1//4

```

However, this does not seem to work when type instabilities are involved:

```julia
julia> @inferred pmap3(x->x%2==0 ? 0 : 0.1, [1, 2])
ERROR: return type Array{Float64,1} does not match inferred return type Array{_A,1} where _A
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] top-level scope at REPL[5]:1

```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [January 6, 2020, 2:58pm UTC](https://discourse.julialang.org/t/tutorial-on-using-advanced-type-system-in-julia/32903/26 "2020-01-06T14:58:03Z")

</div>

No, it will not (yet) infer a general result type that is a Union.  
However, `typeof` will abstract:

```julia
julia> typeof( map(x->x%2==0 ? 0 : 0.1, [1, 2]) )
Array{Real,1}

julia> typeof( map(x->x%2==0 ? 0.0f0 : 0.1, [1, 2]) )
Array{AbstractFloat,1}

```

---

<div class="post-metadata">

**Author:** ![tro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tro3/32/12355_2.png) [@tro3](https://discourse.julialang.org/u/tro3)\
**Post date:** [January 6, 2020, 5:54pm UTC](https://discourse.julialang.org/t/tutorial-on-using-advanced-type-system-in-julia/32903/27 "2020-01-06T17:54:36Z")

</div>

> [@ffevotte](#):
>
> Also, I’m not sure that the two-stage process is needed.

You are correct. I noticed the same thing a little after the above posting and folded it in.

> [@ffevotte](#):
>
> typeof(fn(first(itr)))

This is cute! I’ll go with this.

> [@ffevotte](#):
>
> However, this does not seem to work when type instabilities are involved

Actually, I think it still does (or is at least the same). The error you are seeing is the `@inferred` macro (properly) complaining about type stability. I get the same result with the longer `map` version.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [January 6, 2020, 5:59pm UTC](https://discourse.julialang.org/t/tutorial-on-using-advanced-type-system-in-julia/32903/28 "2020-01-06T17:59:59Z")

</div>

Two quick things:

- Because of some quirky compiler trade-offs, `pmap2(fn, itr) = pmap2(fn, itr, eltype(typeof(map(fn, []))))` will keep `fn` “generic”, and will not specialize on it. Use `pmap2(fn::F, iter) where F = ...` Function and DataTypes are the only objects treated this way.
- Instead of calling `return_type`, consider whether your problem can’t be solved with type widening via [GitHub - JuliaFolds/BangBang.jl: Immutables as mutables, mutables as immutables.](https://github.com/tkf/BangBang.jl)

---

<div class="post-metadata">

**Author:** ![terasakisatoshi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/terasakisatoshi/32/9187_2.png) [@terasakisatoshi](https://discourse.julialang.org/u/terasakisatoshi)\
**Post date:** [January 27, 2020, 10:15am UTC](https://discourse.julialang.org/t/tutorial-on-using-advanced-type-system-in-julia/32903/29 "2020-01-27T10:15:53Z")

</div>

> Is there a tutorial on advanced use of types in Julia? The manual isn’t enough for me to read advanced abstract type usage in actual packages.

I think [Quantitative Economics with Julia](https://discourse.julialang.org/uploads/short-url/jNT82jRdHz2exnGCe0oSZA74Oq5.png) might be helpful who would like to study Julia, especially type system.

[https://julia.quantecon.org/index\_toc.html](https://julia.quantecon.org/index_toc.html)

[Previous page](https://discourse.julialang.org/t/tutorial-on-using-advanced-type-system-in-julia/32903.md?page=1)
