DataType with type parameter

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?

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:

function do_this(x::T, y::T) where T
    if T <: AbstractFloat
        x + y
        x - y

and have this behavior

julia> do_this(1.0, 2.0)

julia> do_this(1,2)

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.

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

It’s not possible.



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

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 {
; 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 {
; 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

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 section that I refer to often myself.

1 Like