I love how easy it is to view the machine code for a function. Julia actually ships with a handful of these introspection macros: <a href="https://docs.julialang.org/en/v1/devdocs/reflection/#Intermediate-and-compiled-representations" rel="nofollow">https://docs.julialang.org/en/v1/devdocs/reflection/#Interme...</a>. They are invaluable when it comes to optimization.
This reminds me of these slides on the Julia compiler and Zygote [1] that really impressed me at the time. Not only the compiler is incredibly smart, it also gives the programmer to a lot of tools to talk with it (the introspection macros, IR Tools, macros on AST, macros on IR - generated functions).<p>And maybe a little related, while traits are not a feature on the language, you can use this kind of compile-time optimizations to implement trait dispatch that the compiler will completely erase from the generated code, the so called holy traits [2].<p>[1] <a href="https://docs.google.com/presentation/d/1IiBLVU5Pj-48vzEMnuYEr9WQy9u2oi6Yk_8F3sgGkvQ/preview#slide=id.p" rel="nofollow">https://docs.google.com/presentation/d/1IiBLVU5Pj-48vzEMnuYE...</a><p>[2] <a href="https://invenia.github.io/blog/2019/11/06/julialang-features-part-2/" rel="nofollow">https://invenia.github.io/blog/2019/11/06/julialang-features...</a>
This is a good feature! Sometimes, you want to use `if` `else` based on type information, say if your function is very large and the different behavior based off of type is only a small piece of the process you want to convey.<p>It's great to have the flexibility.
I am not a Julia user, can the compiler optimize away the run-time type checking when `foo`'s argument is not easily known at compile time?<p>e.g.,<p><pre><code> foo(function_which_returns_dynamic_type())</code></pre>
This only shows that it can do method type specialization to get rid of type dispatch.<p>Can you add a new multimethod clause to an existing if-then-else type dispatch method? For example add:<p><pre><code> foo(x::Baz) = x.foo
</code></pre>
Or is harm still done because the method is not open for extension when written like that?