Type stability for returning abstract types

  1. Inferring an abstract type instead of a union would be worse because you can always add more subtypes. With a union there’s a limited number of options that can be explicitly handled. This happens on 0.7: efficient code for each option in the union (for small unions) is generated and a single branch decides which is run based upon the value.

  2. So yes, there is a higher performance cost to an abstract type than a small union since more abstract types can always be added and Julia can’t enumerate all options up front.

But on Julia 0.7, constant propagation to inlined functions completely removes this problem — and you don’t need to use Val wrappers at all. But I just copied your code verbatim and this works:

julia> g() = f(:AA)
g (generic function with 1 method)

julia> @code_warntype g()
Variables:

Body:
  begin
      return $(QuoteNode(A()))
  end::A

julia> h() = f(:BB)
h (generic function with 1 method)

julia> @code_warntype h()
Variables:

Body:
  begin
      return $(QuoteNode(B()))
  end::B
7 Likes