@@ -7,22 +7,42 @@ struct Static{N} <: Integer
7
7
Static {N} () where {N} = new {N::Int} ()
8
8
end
9
9
Base. @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} ()
11
12
Static (:: Val{N} ) where {N} = Static {N} ()
12
13
Base. Val (:: Static{N} ) where {N} = Val {N} ()
13
14
Base. convert (:: Type{T} , :: Static{N} ) where {T<: Number ,N} = convert (T, N)
14
15
Base. 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
18
38
Base.:(% )(:: Static{N} , :: Type{Integer} ) where {N} = N
19
39
20
40
Base. iszero (:: Static{0} ) = true
21
41
Base. iszero (:: Static ) = false
22
42
Base. isone (:: Static{1} ) = true
23
43
Base. isone (:: Static ) = false
24
44
25
- for T ∈ [:Any , :Number , :Integer ]
45
+ for T = [:Real , :Rational , :Integer ]
26
46
@eval begin
27
47
@inline Base.:(+ )(i:: $T , :: Static{0} ) = i
28
48
@inline Base.:(+ )(i:: $T , :: Static{M} ) where {M} = i + M
@@ -38,18 +58,28 @@ for T ∈ [:Any, :Number, :Integer]
38
58
@inline Base.:(* )(:: Static{M} , i:: $T ) where {M} = M * i
39
59
end
40
60
end
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
+
41
67
@inline Base.:(* )(:: Static{0} , :: Static{0} ) = Static {0} ()
42
68
@inline Base.:(* )(:: Static{1} , :: Static{0} ) = Static {0} ()
43
69
@inline Base.:(* )(:: Static{0} , :: Static{1} ) = Static {0} ()
44
70
@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} ()
45
75
for f ∈ [:(+ ), :(- ), :(* ), :(/ ), :(÷ ), :(% ), :(<< ), :(>> ), :(>>> ), :(& ), :(| ), :(⊻ )]
46
76
@eval @generated Base.$ f (:: Static{M} , :: Static{N} ) where {M,N} = Expr (:call , Expr (:curly , :Static , $ f (M, N)))
47
77
end
48
78
for f ∈ [:(== ), :(!= ), :(< ), :(≤ ), :(> ), :(≥ )]
49
79
@eval begin
50
80
@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)
53
83
end
54
84
end
55
85
0 commit comments