Skip to content

can't infer type in some constructors #23

@kleinschmidt

Description

@kleinschmidt
julia> @code_warntype ConjugatePriors.NormalInverseGamma(1., 1., 1., 1.)
Variables:
  #self# <optimized out>
  mu::Float64
  v0::Float64
  sh::Float64
  r::Float64
  T::Any

Body:
  begin 
      T::Any = (Base.promote_type)(Float64, (Base.promote_type)(Float64, (Base.promote_type)(Float64, Float64)::Any)::Any)::Any # line 20:
      return ((Core.apply_type)(ConjugatePriors.NormalInverseGamma, T::Any)::Type{ConjugatePriors.NormalInverseGamma{_}} where _)((T::Any)(mu::Float64)::Any, (T::Any)(v0::Float64)::Any, (T::Any)(sh::Float64)::Any, (T::Any)(r::Float64)::Any)::ConjugatePriors.NormalInverseGamma{_} where _
  end::ConjugatePriors.NormalInverseGamma{_} where _

I think this comes from having constructors like

function NormalInverseGamma(mu::Real, v0::Real, sh::Real, r::Real)
T = promote_type(typeof(mu), typeof(v0), typeof(sh), typeof(r))
return NormalInverseGamma{T}(T(mu),T(v0),T(sh),T(r))
end

where the type is computed at runtime (with promote_type), but I could be wrong about that.

When all the arguments are of the same type, adding a method like NormalInverseGamma(::T, ::T, ::T, ::T) where {T<:Real}) gets rid of the type instability. Another possible fix would be to use the inner constructor with the explicit type parameter whenever possible (e.g., in posterior_canon, which is where I was bitten by this).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions