# DataType with type parameter

**URL:** <https://discourse.julialang.org/t/datatype-with-type-parameter/17932>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 23, 2018, 10:56pm UTC](https://discourse.julialang.org/t/datatype-with-type-parameter/17932 "2018-11-23T22:56:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [November 23, 2018, 10:56pm UTC](https://discourse.julialang.org/t/datatype-with-type-parameter/17932/1 "2018-11-23T22:56:01Z")

</div>

DataType is subtype of Type{T} but where Type{T} has a typeparameter, DataType does not have it.

How is this possible?  
More practically, how can I dispatch on DataType, while extracting the Typeparameter?

---

<div class="post-metadata">

**Author:** ![alec-hoyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec-hoyland/32/6001_2.png) [@alec-hoyland](https://discourse.julialang.org/u/alec-hoyland)\
**Post date:** [November 24, 2018, 1:43am UTC](https://discourse.julialang.org/t/datatype-with-type-parameter/17932/2 "2018-11-24T01:43:54Z")

</div>

When you want to pass arguments to a function in Julia, the proper method for those arguments is compiled. This is why you can write something like this:

```julia
function do_this(x::T, y::T) where T
    if T <: AbstractFloat
        x + y
    else
        x - y
    end
end

```

and have this behavior

```julia
julia> do_this(1.0, 2.0)
3.0

julia> do_this(1,2)
-1

```

The type of of the arguments determines the specific method called. As long as the types of your variables don’t change, this will make your code fast and stable.

In this example, `T` is never actually defined – it’s just used symbolically within the function definition. That way, if `x` and `y` happen to be some arbitrary type `T`, the correct method will be generated. As you can see, within the actual function definition, `T` can be used.

---

<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:** [November 24, 2018, 1:00pm UTC](https://discourse.julialang.org/t/datatype-with-type-parameter/17932/3 "2018-11-24T13:00:29Z")

</div>

`Type` is a trick to express things such as what type parameters a type has.

> [@schlichtanders](#):
>
> More practically, how can I dispatch on DataType, while extracting the Typeparameter?

It’s not possible.

X-ref: [redesign typeof(::Type) · Issue #29368 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/29368)

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [November 26, 2018, 10:25am UTC](https://discourse.julialang.org/t/datatype-with-type-parameter/17932/4 "2018-11-26T10:25:48Z")

</div>

just looked at the respective compiled code and it looks really good indeed,  
everything is stripped off

```julia
julia> @code_llvm do_this(1.0, 2.0)

; Function do_this
; Location: REPL[1]:2
; Function Attrs: uwtable
define double @julia_do_this_35437(double, double) #0 {
top:
; Location: REPL[1]:3
; Function +; {
; Location: float.jl:395
  %2 = fadd double %0, %1
;}
  ret double %2
}

julia> @code_llvm do_this(1,2)

; Function do_this
; Location: REPL[1]:2
; Function Attrs: uwtable
define i64 @julia_do_this_35439(i64, i64) #0 {
top:
; Location: REPL[1]:5
; Function -; {
; Location: int.jl:52
  %2 = sub i64 %0, %1
;}
  ret i64 %2
}

```

it would be awesome to have an overview, how far this goes, i.e. what if/else/try/catch/for/while kind of control flows are allowed for the compiler to still cleanly simplify the code

---

<div class="post-metadata">

**Author:** ![alec-hoyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec-hoyland/32/6001_2.png) [@alec-hoyland](https://discourse.julialang.org/u/alec-hoyland)\
**Post date:** [November 26, 2018, 9:28pm UTC](https://discourse.julialang.org/t/datatype-with-type-parameter/17932/5 "2018-11-26T21:28:23Z")

</div>

If you’re looking for a visualization of this kind of profiling, you can use the `Profiler()` function or `@Profiler` macro from the `Juno.jl` package to create a flame chart that shows resource use. You can also use `@profile` from the `Profiler.jl` package. I have no idea whether anyone has seriously benchmarked control flow expressions. I think generally as long as you function-barrier things or use `const` parameters (and check your code with `@code_warntype`), your code should be very fast. The docs have an extensive [performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/) section that I refer to often myself.
