Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't infer type in some constructors #23

Open
kleinschmidt opened this issue Jan 22, 2018 · 0 comments
Open

can't infer type in some constructors #23

kleinschmidt opened this issue Jan 22, 2018 · 0 comments

Comments

@kleinschmidt
Copy link
Member

kleinschmidt commented Jan 22, 2018

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant