# Promote and infinite recursion

**URL:** <https://discourse.julialang.org/t/promote-and-infinite-recursion/867>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 11, 2016, 7:26pm UTC](https://discourse.julialang.org/t/promote-and-infinite-recursion/867 "2016-12-11T19:26:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 11, 2016, 7:26pm UTC](https://discourse.julialang.org/t/promote-and-infinite-recursion/867/1 "2016-12-11T19:26:26Z")

</div>

When reading the code of [IntervalSets.jl](https://github.com/JuliaMath/IntervalSets.jl/blob/master/src/IntervalSets.jl#L28) I saw a constructor defined like this:

```julia
checked_conversion{T}(::Type{T}, a, b) = _checked_conversion(T, convert(T, a), convert(T, b))
_checked_conversion{T}(::Type{T}, a::T, b::T) = a, b
_checked_conversion{T}(::Type{T}, a, b) = throw(ArgumentError("$a and $b are not both of type $T"))

immutable ClosedInterval{T} <: AbstractInterval{T}
    left::T
    right::T

    ClosedInterval(l::T, r::T) = new(l, r)
end

ClosedInterval{T}(left::T, right::T) = ClosedInterval{T}(left, right)
(::Type{ClosedInterval{T}}){T}(left, right) =
    ClosedInterval{T}(checked_conversion(T, left, right)...)

function ClosedInterval(left, right)
    # Defining this as ClosedInterval(promote(left, right)...) has one problem:
    # if left and right do not promote to a common type, it triggers a StackOverflow.
    T = promote_type(typeof(left), typeof(right))
    ClosedInterval{T}(checked_conversion(T, left, right)...)
end

```

My question: is this the current best practice? Reading [the manual on parametric constructors](http://docs.julialang.org/en/latest/manual/constructors/#parametric-constructors) I was not aware of the issue.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [December 14, 2016, 6:04pm UTC](https://discourse.julialang.org/t/promote-and-infinite-recursion/867/2 "2016-12-14T18:04:25Z")

</div>

It seems reasonable to me. It looks like this is similar to the handling at [julia/promotion.jl at bfc72b6fe913236ccb2bdd1ef254bceccb0cc31a · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/bfc72b6fe913236ccb2bdd1ef254bceccb0cc31a/base/promotion.jl#L167-L179). I see there’s still a TODO there though about trying to handle this StackOverflow better. Perhaps the ideas from that code could be used to help improve it?
