@@ -7,22 +7,42 @@ struct Static{N} <: Integer
77 Static {N} () where {N} = new {N::Int} ()
88end
99Base. @pure Static (N:: Int ) = Static {N} ()
10- Static (N) = Static (convert (Int, N))
10+ Static (N:: Integer ) = Static (convert (Int, N))
11+ Static (:: Static{N} ) where {N} = Static {N} ()
1112Static (:: Val{N} ) where {N} = Static {N} ()
1213Base. Val (:: Static{N} ) where {N} = Val {N} ()
1314Base. convert (:: Type{T} , :: Static{N} ) where {T<: Number ,N} = convert (T, N)
1415Base. convert (:: Type{Static{N}} , :: Static{N} ) where {N} = Static {N} ()
15- Base. promote_rule (:: Type{<:Static} , :: Type{T} ) where {T} = promote_rule (Int, T)
16- Base. promote_rule (:: Type{T} , :: Type{<:Static} ) where {T} = promote_rule (T, Int)
17- Base. promote_rule (:: Type{<:Static} , :: Type{<:Static} ) where {T} = Int
16+ for S ∈ [:Any , :AbstractIrrational ]# , :(Complex{<:Real})]
17+ # let S = :Any
18+ @eval begin
19+ Base. promote_rule (:: Type{<:Static} , :: Type{T} ) where {T <: $S } = promote_rule (Int, T)
20+ Base. promote_rule (:: Type{T} , :: Type{<:Static} ) where {T <: $S } = promote_rule (T, Int)
21+ end
22+ end
23+ for (S,T) ∈ [(:Complex ,:Real ), (:Rational , :Integer ), (:(Base. TwicePrecision),:Any )]
24+ @eval Base. promote_rule (:: Type{$S{T}} , :: Type{<:Static} ) where {T <: $T } = promote_rule ($ S{T}, Int)
25+ end
26+ Base. promote_rule (:: Type{Union{Nothing,Missing}} , :: Type{<:Static} ) = Union{Nothing, Missing, Int}
27+ Base. promote_rule (:: Type{T} , :: Type{<:Static} ) where {T >: Union{Missing,Nothing} } = promote_rule (T, Int)
28+ Base. promote_rule (:: Type{T} , :: Type{<:Static} ) where {T >: Nothing } = promote_rule (T, Int)
29+ Base. promote_rule (:: Type{T} , :: Type{<:Static} ) where {T >: Missing } = promote_rule (T, Int)
30+ for T ∈ [:Bool , :Missing , :BigFloat , :BigInt , :Nothing ]
31+ # let S = :Any
32+ @eval begin
33+ Base. promote_rule (:: Type{<:Static} , :: Type{$T} ) = promote_rule (Int, $ T)
34+ Base. promote_rule (:: Type{$T} , :: Type{<:Static} ) = promote_rule ($ T, Int)
35+ end
36+ end
37+ Base. promote_rule (:: Type{<:Static} , :: Type{<:Static} ) = Int
1838Base.:(% )(:: Static{N} , :: Type{Integer} ) where {N} = N
1939
2040Base. iszero (:: Static{0} ) = true
2141Base. iszero (:: Static ) = false
2242Base. isone (:: Static{1} ) = true
2343Base. isone (:: Static ) = false
2444
25- for T ∈ [:Any , :Number , :Integer ]
45+ for T = [:Real , :Rational , :Integer ]
2646 @eval begin
2747 @inline Base.:(+ )(i:: $T , :: Static{0} ) = i
2848 @inline Base.:(+ )(i:: $T , :: Static{M} ) where {M} = i + M
@@ -38,18 +58,28 @@ for T ∈ [:Any, :Number, :Integer]
3858 @inline Base.:(* )(:: Static{M} , i:: $T ) where {M} = M * i
3959 end
4060end
61+ @inline Base.:(+ )(:: Static{0} , :: Static{0} ) = Static {0} ()
62+ @inline Base.:(+ )(:: Static{0} , :: Static{M} ) where {M} = Static {M} ()
63+ @inline Base.:(+ )(:: Static{M} , :: Static{0} ) where {M} = Static {M} ()
64+
65+ @inline Base.:(- )(:: Static{M} , :: Static{0} ) where {M} = Static {M} ()
66+
4167@inline Base.:(* )(:: Static{0} , :: Static{0} ) = Static {0} ()
4268@inline Base.:(* )(:: Static{1} , :: Static{0} ) = Static {0} ()
4369@inline Base.:(* )(:: Static{0} , :: Static{1} ) = Static {0} ()
4470@inline Base.:(* )(:: Static{1} , :: Static{1} ) = Static {1} ()
71+ @inline Base.:(* )(:: Static{M} , :: Static{0} ) where {M} = Static {0} ()
72+ @inline Base.:(* )(:: Static{0} , :: Static{M} ) where {M} = Static {0} ()
73+ @inline Base.:(* )(:: Static{M} , :: Static{1} ) where {M} = Static {M} ()
74+ @inline Base.:(* )(:: Static{1} , :: Static{M} ) where {M} = Static {M} ()
4575for f ∈ [:(+ ), :(- ), :(* ), :(/ ), :(÷ ), :(% ), :(<< ), :(>> ), :(>>> ), :(& ), :(| ), :(⊻ )]
4676 @eval @generated Base.$ f (:: Static{M} , :: Static{N} ) where {M,N} = Expr (:call , Expr (:curly , :Static , $ f (M, N)))
4777end
4878for f ∈ [:(== ), :(!= ), :(< ), :(≤ ), :(> ), :(≥ )]
4979 @eval begin
5080 @inline Base.$ f (:: Static{M} , :: Static{N} ) where {M,N} = $ f (M, N)
51- @inline Base.$ f (:: Static{M} , x:: Integer ) where {M} = $ f (M, x)
52- @inline Base.$ f (x:: Integer , :: Static{M} ) where {M} = $ f (x, M)
81+ @inline Base.$ f (:: Static{M} , x:: Int ) where {M} = $ f (M, x)
82+ @inline Base.$ f (x:: Int , :: Static{M} ) where {M} = $ f (x, M)
5383 end
5484end
5585
0 commit comments