Hi,
I have a struct with 2 Vector fields holding Tuples. When creating a new object I want the Vectors to be sorted but for some reason it’s not working.
abstract type Lifecycle end
struct Restorable <: Lifecycle
health::Health
damage_thresholds::Vector{Tuple{Percentage, Float64}}
restoration_thresholds::Vector{Tuple{Percentage, Float64}}
end
Restorable(
health::Real = 1;
damage_thresholds::Vector = Vector{Tuple{Percentage,Real}}(),
restoration_thresholds::Vector = Vector{Tuple{Percentage,Real}}(),
) = Restorable(Health(health), sort(damage_thresholds), sort(restoration_thresholds))
When I create a new Restorable with the following code:
Restorable(0.8, [(1.0, 0.2), (0.7, 0.5)], [(1.0, 0.3), (0.3, 0.1), (0.7, 0.2)])
I get:
Restorable(Health(80.0%), Tuple{Percentage,Float64}[(100.0%, 0.2), (70.0%, 0.5)], Tuple{Percentage,Float64}[(100.0%, 0.3), (30.0%, 0.1), (70.0%, 0.2)])
where the second Vector has not been sorted.
I tested whether sorting of Percentage vectors works and it does. Both with just Percentage elements and with Tuples where the first element is a Percentage. What am I missing?
For completeness, here is the code for Percentage and Health:
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))
Thanks in advance,
Stef