I’m still a bit stuck up.
I would like to be able to store a restriction (range checking) but also the lack of range checking.
I did
abstract type AbstractRangeNumber end
Number(rn::AbstractRangeNumber) = rn.v
struct NoRangeNumber{T<:Number} <: AbstractRangeNumber
v::T
function NoRangeNumber(v::Number)
return new{typeof(v)}(v)
end
end
"""
RangeNumber(v, r)
# Example
a = RangeNumber(3, 1:7)
"""
struct RangeNumber{T<:Number, R} <: AbstractRangeNumber
v::T
function RangeNumber(v::Number, r::UnitRange)
v in r || error("$v not in $r")
return new{typeof(v), r}(v)
end
end
RangeNumber{T, R}(n) where {T, R} = RangeNumber(n, R)
Base.:+(a::RangeNumber{T, R}, b::RangeNumber{T, R}) where {T, R} = RangeNumber(a.v+b.v, R)
Base.:+(a::Number, b::RangeNumber{T, R}) where {T, R} = RangeNumber(a+b.v, R)
Base.:+(a::RangeNumber{T, R}, b::Number) where {T, R} = RangeNumber(a.v+b, R)
Base.:(==)(a::AbstractRangeNumber{T, R}, b::AbstractRangeNumber{T, R}) where {T, R} = a.v == b.v
Base.:(==)(a::Number, b::AbstractRangeNumber{T, R}) where {T, R} = a == b.v
Base.:(==)(a::AbstractRangeNumber{T, R}, b::Number) where {T, R} = a.v == b
and tests:
using Nextion: RangeNumber, NoRangeNumber
using Test
@testset "RangeNumber" begin
@testset "construct / isequal" begin
a = RangeNumber(3, 1:7)
@test a == 3
end
@testset "addition / overflow" begin
a = RangeNumber(3, 1:7)
@test a + a == 6
@test_throws ErrorException a + a + a == 9
end
@testset "other construct" begin
a = RangeNumber{Int64, 1:7}(3)
@test a == 3
@test_throws ErrorException a = RangeNumber{Int64, 1:7}(8)
end
@testset "NoRangeNumber" begin
a = NoRangeNumber(3)
@test a == 3
end
end
but it’s failling for odd reason
$ julia test/test_range_number.jl
ERROR: LoadError: LoadError: TypeError: in Type{...} expression, expected UnionAll, got Type{Nextion.AbstractRangeNumber}
Stacktrace:
[1] top-level scope at none:0
[2] include at ./boot.jl:317 [inlined]
[3] include_relative(::Module, ::String) at ./loading.jl:1038
[4] include at ./sysimg.jl:29 [inlined]
[5] include(::String) at /home/scls/.julia/dev/Nextion/src/Nextion.jl:37
[6] top-level scope at none:0
[7] include at ./boot.jl:317 [inlined]
[8] include_relative(::Module, ::String) at ./loading.jl:1038
[9] include(::Module, ::String) at ./sysimg.jl:29
[10] top-level scope at none:2
[11] eval at ./boot.jl:319 [inlined]
[12] eval(::Expr) at ./client.jl:399
[13] top-level scope at ./none:3
The general idea is to be able to store either a restriction or a lack of restriction in numbers.