# Crashed Julia with \<

**URL:** <https://discourse.julialang.org/t/crashed-julia-with/49831>\
**Category:** New to Julia\
**Created:** [November 9, 2020, 11:21am UTC](https://discourse.julialang.org/t/crashed-julia-with/49831 "2020-11-09T11:21:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [November 9, 2020, 11:21am UTC](https://discourse.julialang.org/t/crashed-julia-with/49831/1 "2020-11-09T11:21:28Z")

</div>

Hi,

I have defined the following two types:

```julia
struct Percentage <: Real
    value::Float64
    Percentage(x) = x < 0 ? new(0) : x > 1 ? new(1) : new(round(x, digits = 6))
end

Base.show(io::IO, x::Percentage) = print(io, "$(x.value * 100)%")

Base.convert(::Type{Percentage}, x::Real) = Percentage(x)
Base.convert(::Type{Percentage}, x::Percentage) = x

Base.promote_rule(::Type{T}, ::Type{Percentage}) where T <: Real = Percentage

import Base: +, -, <, >, <=, >=, ==

+(x::Percentage, y::Percentage) = Percentage(x.value + y.value)
-(x::Percentage) = Percentage(-x.value)
-(x::Percentage, y::Percentage) = Percentage(x.value - y.value)
<(x::Percentage, y::Percentage) = x.value < y.value
<=(x::Percentage, y::Percentage) = x.value <= y.value
>(x::Percentage, y::Percentage) = x.value > y.value
>=(x::Percentage, y::Percentage) = x.value >= y.value
==(x::Percentage, y::Percentage) = x.value == y.value

Base.max(x::Percentage, y::Percentage) = Percentage(max(x.value, y.value))
Base.min(x::Percentage, y::Percentage) = Percentage(min(x.value, y.value))

mutable struct Health <: Real
    current::Percentage
    Health(current=1) = new(current)
end

Base.convert(::Type{Health}, x::Real) = Health(x)
Base.convert(::Type{Health}, x::Health) = x

Base.promote_rule(::Type{T}, ::Type{Health}) where T <: Real = Health

import Base: +, -, <, >, <=, >=, ==

+(x::Health, y::Health) = Health(x.current + y.current)
-(x::Health) = Health(-x.current)
-(x::Health, y::Health) = Health(x.current - y.current)
<(x::Health, y::Health) = x.current < y.current
<=(x::Health, y::Health) = x.current <= y.current
>(x::Health, y::Health) = x.current > y.current
>=(x::Health, y::Health) = x.current >= y.current
==(x::Health, y::Health) = x.current == y.current

Base.max(x::Health, y::Health) = Health(max(x.current, y.current))
Base.min(x::Health, y::Health) = Health(min(x.current, y.current))

```

When I type

```julia
Health(0.1) < Percentage(0.2)

```

in the REPL, I get the message that Julia has exited.

I’m using JuliaPro 1.5.2-2 on Mac OS X 10.15.7

Is there anyone who has an idea what is going on?

Thanks in advance,  
Stef

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [November 9, 2020, 11:35am UTC](https://discourse.julialang.org/t/crashed-julia-with/49831/2 "2020-11-09T11:35:46Z")

</div>

You have specified that `Health` and `Percentage` take precedence over other `Real` types but have not specified _which of them_ takes precedence. As both are subtypes of `Real` themselves, that triggers an error.

On Linux, I get stack overflow from promotions.

In short, you need `promote_rule(::Type{Percentage}, ::Type{Health})`.

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [November 9, 2020, 11:57am UTC](https://discourse.julialang.org/t/crashed-julia-with/49831/3 "2020-11-09T11:57:09Z")

</div>

Apparently I needed to reverse the order of the types to solve it.

```julia
promote_rule(::Type{Health}, ::Type{Percentage})

```
