Skip to content

Commit 418c08d

Browse files
authored
Support public interface for non-exported (#143)
Support public interface for non-exported using SciMLPublic.jl, particularly those methods used in downstream packages. Add more documentation and improve consistency.
1 parent b50279c commit 418c08d

File tree

4 files changed

+150
-34
lines changed

4 files changed

+150
-34
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
name = "Static"
22
uuid = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
33
authors = ["chriselrod", "ChrisRackauckas", "Tokazama"]
4-
version = "1.2.0"
4+
version = "1.3.0"
55

66
[deps]
77
CommonWorldInvalidations = "f70d9fcc-98c5-4d4a-abd7-e4cdeebd8ca8"
88
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
99
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
10+
SciMLPublic = "431bcebd-1456-4ced-9d72-93c2757fff0b"
1011

1112
[compat]
1213
Aqua = "0.8.4"
1314
CommonWorldInvalidations = "1"
1415
IfElse = "0.1"
1516
PrecompileTools = "1.1"
17+
SciMLPublic = "1.0.0"
1618
Test = "1"
1719
julia = "1.10"
1820

src/Static.jl

Lines changed: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
module Static
22

33
import IfElse: ifelse
4+
using SciMLPublic: @public
45

56
export StaticInt, StaticFloat64, StaticSymbol, True, False, StaticBool, NDIndex
6-
export dynamic, is_static, known, static, static_promote
7+
export dynamic, is_static, known, static, static_promote, static_first, static_step,
8+
static_last
9+
10+
@public OptionallyStaticRange,
11+
OptionallyStaticUnitRange, OptionallyStaticStepRange, SUnitRange, SOneTo
12+
@public eachop, eachop_tuple, reduce_tup, eq, ne, gt, ge, le, lt, mul, add
713

814
import PrecompileTools: @recompile_invalidations
915

@@ -12,7 +18,7 @@ import PrecompileTools: @recompile_invalidations
1218
end
1319

1420
"""
15-
StaticSymbol
21+
StaticSymbol(S::Symbol)::StaticSymbol{S}
1622
1723
A statically typed `Symbol`.
1824
"""
@@ -33,7 +39,7 @@ Base.Symbol(@nospecialize(s::StaticSymbol)) = known(s)
3339
abstract type StaticInteger{N} <: Number end
3440

3541
"""
36-
StaticBool(x::Bool) -> True/False
42+
StaticBool(x::Bool)::Union{True, False}
3743
3844
A statically typed `Bool`.
3945
"""
@@ -55,7 +61,7 @@ function StaticBool(x::Bool)
5561
end
5662

5763
"""
58-
StaticInt(N::Int) -> StaticInt{N}()
64+
StaticInt(N::Int)::StaticInt{N}
5965
6066
A statically sized `Int`.
6167
Use `StaticInt(N)` instead of `Val(N)` when you want it to behave like a number.
@@ -68,7 +74,7 @@ struct StaticInt{N} <: StaticInteger{N}
6874
end
6975

7076
"""
71-
IntType(x::Integer) -> Union{Int,StaticInt}
77+
IntType(x::Integer)::Union{Int, StaticInt}
7278
7379
`IntType` is a union of `Int` and `StaticInt`. As a function, it ensures that `x` one of the
7480
two.
@@ -256,9 +262,10 @@ function static(x::X) where {X}
256262
end
257263

258264
"""
259-
is_static(::Type{T}) -> StaticBool
265+
is_static(::Type{T})::Union{True, False}
260266
261-
Returns `True` if `T` is a static type.
267+
If `T` is a static type return `static(true)::True` and otherwise returns
268+
`static(false)::False`
262269
263270
See also: [`static`](@ref), [`known`](@ref)
264271
"""
@@ -582,7 +589,7 @@ permute(@nospecialize(x::Tuple), @nospecialize(perm::Val)) = permute(x, static(p
582589
end
583590

584591
"""
585-
eachop(op, args...; iterator::Tuple{Vararg{StaticInt}}) -> Tuple
592+
Static.eachop(op, args...; iterator::Tuple{Vararg{StaticInt}})::Tuple
586593
587594
Produces a tuple of `(op(args..., iterator[1]), op(args..., iterator[2]),...)`.
588595
"""
@@ -592,7 +599,7 @@ end
592599
eachop(::F, ::Tuple{}, args::Vararg{Any}) where {F} = ()
593600

594601
"""
595-
eachop_tuple(op, arg, args...; iterator::Tuple{Vararg{StaticInt}}) -> Type{Tuple}
602+
Static.eachop_tuple(op, arg, args...; iterator::Tuple{Vararg{StaticInt}})::Type{Tuple}
596603
597604
Produces a tuple type of `Tuple{op(arg, args..., iterator[1]), op(arg, args..., iterator[2]),...}`.
598605
Note that if one of the arguments passed to `op` is a `Tuple` type then it should be the first argument
@@ -781,66 +788,109 @@ end
781788
end
782789

783790
"""
784-
eq(x, y)
791+
Static.eq(x, y)::Union{Bool, True, False}
785792
786-
Equivalent to `!=` but if `x` and `y` are both static returns a `StaticBool.
793+
Equivalent to `==` but if `x` and `y` are static the return value is a `StaticBool.
787794
"""
788795
eq(x::X, y::Y) where {X, Y} = ifelse(is_static(X) & is_static(Y), static, identity)(x == y)
796+
797+
"""
798+
Static.eq(x)::Base.Fix2{typeof(Static.eq}}
799+
800+
Create a function that compares `x` to other values using `Static.eq` (i.e. a
801+
function equivalent to `y -> Static.eq(y, x)`).
802+
"""
789803
eq(x) = Base.Fix2(eq, x)
790804

791805
"""
792-
ne(x, y)
806+
Static.ne(x, y)::Union{Bool, True, False}
793807
794-
Equivalent to `!=` but if `x` and `y` are both static returns a `StaticBool.
808+
Equivalent to `!=` but if `x` and `y` are static the return value is a `StaticBool.
795809
"""
796810
ne(x::X, y::Y) where {X, Y} = !eq(x, y)
811+
812+
"""
813+
Static.ne(x)::Base.Fix2{typeof(Static.ne}}
814+
815+
Create a function that compares `x` to other values using `Static.ne` (i.e. a
816+
function equivalent to `y -> Static.ne(y, x))`.
817+
"""
797818
ne(x) = Base.Fix2(ne, x)
798819

799820
"""
800-
gt(x, y)
821+
Static.ne(x, y)::Union{Bool, True, False}
801822
802-
Equivalent to `>` but if `x` and `y` are both static returns a `StaticBool.
823+
Equivalent to `>` but if `x` and `y` are static the return value is a `StaticBool.
803824
"""
804825
gt(x::X, y::Y) where {X, Y} = ifelse(is_static(X) & is_static(Y), static, identity)(x > y)
826+
827+
"""
828+
Static.gt(x)::Base.Fix2{typeof(Static.gt}}
829+
830+
Create a function that compares `x` to other values using `Static.gt` (i.e. a
831+
function equivalent to `y -> Static.gt(y, x))`.
832+
"""
805833
gt(x) = Base.Fix2(gt, x)
806834

807835
"""
808-
ge(x, y)
836+
Static.ge(x, y)::Union{Bool, True, False}
809837
810-
Equivalent to `>=` but if `x` and `y` are both static returns a `StaticBool.
838+
Equivalent to `>=` but if `x` and `y` are static the return value is a `StaticBool.
811839
"""
812840
ge(x::X, y::Y) where {X, Y} = ifelse(is_static(X) & is_static(Y), static, identity)(x >= y)
841+
842+
"""
843+
Static.ge(x)::Base.Fix2{typeof(Static.ge}}
844+
845+
Create a function that compares `x` to other values using `Static.ge` (i.e. a
846+
function equivalent to `y -> Static.ge(y, x)`).
847+
"""
813848
ge(x) = Base.Fix2(ge, x)
814849

815850
"""
816-
le(x, y)
851+
Static.le(x, y)::Union{Bool, True, False}
817852
818-
Equivalent to `<=` but if `x` and `y` are both static returns a `StaticBool.
853+
Equivalent to `<=` but if `x` and `y` are static the return value is a `StaticBool.
819854
"""
820855
le(x::X, y::Y) where {X, Y} = ifelse(is_static(X) & is_static(Y), static, identity)(x <= y)
856+
857+
"""
858+
Static.le(x)::Base.Fix2{typeof(Static.le}}
859+
860+
Create a function that compares `x` to other values using `Static.le` (i.e. a
861+
function equivalent to `y -> Static.le(y, x)`).
862+
"""
821863
le(x) = Base.Fix2(le, x)
822864

823865
"""
824-
lt(x, y)
866+
Static.lt(x, y)::Union{Bool, True, False}
825867
826-
Equivalent to `<` but if `x` and `y` are both static returns a `StaticBool.
868+
Equivalent to `<` but if `x` and `y` are static the return value is a `StaticBool.`
827869
"""
828870
lt(x::X, y::Y) where {X, Y} = ifelse(is_static(X) & is_static(Y), static, identity)(x < y)
871+
872+
"""
873+
Static.lt(x)::Base.Fix2{typeof(Static.lt}}
874+
875+
Create a function that compares `x` to other values using `Static.lt` (i.e. a
876+
function equivalent to y -> Static.lt(y, x)).
877+
"""
829878
lt(x) = Base.Fix2(lt, x)
830879

831880
"""
832-
mul(x) -> Base.Fix2(*, x)
833-
mul(x, y) ->
881+
Static.mul(x)::Base.Fix2{typeof(*)}
834882
835-
Equivalent to `*` but allows for lazy multiplication when passing functions.
883+
Create a function that multiplies `x` with other values (i.e. a function
884+
equivalent to `y -> y * x`).
836885
"""
837886
mul(x) = Base.Fix2(*, x)
838887

839888
"""
840-
add(x) -> Base.Fix2(+, x)
841-
add(x, y) ->
889+
Static.add(x) -> Base.Fix2(+, x)
890+
Static.add(x, y)
842891
843-
Equivalent to `+` but allows for lazy addition when passing functions.
892+
Create a function that adds `x` to other values (i.e. a function equivalent to
893+
`y -> y + x`).
844894
"""
845895
add(x) = Base.Fix2(+, x)
846896

@@ -966,15 +1016,17 @@ end
9661016
return (Base.to_index(A, I[1]), to_indices(A, indstail, Base.tail(I))...)
9671017
end
9681018

969-
function Base.show(io::IO, @nospecialize(x::Union{StaticNumber, StaticSymbol, NDIndex}))
1019+
function Base.show(@nospecialize(io::IO), @nospecialize(x::Union{
1020+
StaticNumber, StaticSymbol, NDIndex}))
9701021
show(io, MIME"text/plain"(), x)
9711022
end
972-
function Base.show(io::IO, ::MIME"text/plain",
973-
@nospecialize(x::Union{StaticNumber, StaticSymbol}))
1023+
function Base.show(
1024+
@nospecialize(io::IO), ::MIME"text/plain", @nospecialize(x::Union{
1025+
StaticNumber, StaticSymbol}))
9741026
print(io, "static(" * repr(known(typeof(x))) * ")")
9751027
nothing
9761028
end
977-
function Base.show(io::IO, m::MIME"text/plain", @nospecialize(x::NDIndex))
1029+
function Base.show(@nospecialize(io::IO), m::MIME"text/plain", @nospecialize(x::NDIndex))
9781030
print(io, "NDIndex")
9791031
show(io, m, Tuple(x))
9801032
nothing

src/float.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
"""
3-
StaticFloat64{N}
3+
StaticFloat64(F::Float64)::StaticFloat64{F}
44
55
A statically sized `Float64`.
66
Use `StaticFloat64(N)` instead of `Val(N)` when you want it to behave like a number.

src/ranges.jl

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,78 @@ const OptionallyStaticRange{
150150
F, L} = Union{OptionallyStaticUnitRange{F, L},
151151
OptionallyStaticStepRange{F, <:Any, L}}
152152

153-
# these probide a generic method for extracting potentially static values.
153+
"""
154+
static_first(x::AbstractRange)
155+
156+
Attempt to return `static(first(x))`, if known at compile time. Otherwise, return
157+
`first(x)`.
158+
159+
See also: [`static_step`](@ref), [`static_last`](@ref)
160+
161+
# Examples
162+
163+
```julia
164+
julia> static_first(static(2):10)
165+
static(2)
166+
167+
julia> static_first(1:10)
168+
1
169+
170+
julia> static_first(Base.OneTo(10))
171+
static(1)
172+
173+
```
174+
"""
154175
static_first(x::Base.OneTo) = StaticInt(1)
155176
static_first(x::Union{Base.Slice, Base.IdentityUnitRange}) = static_first(x.indices)
156177
static_first(x::OptionallyStaticRange) = getfield(x, :start)
157178
static_first(x) = first(x)
158179

180+
"""
181+
static_step(x::AbstractRange)
182+
183+
Attempt to return `static(step(x))`, if known at compile time. Otherwise, return
184+
`step(x)`.
185+
186+
See also: [`static_first`](@ref), [`static_last`](@ref)
187+
188+
# Examples
189+
190+
```julia
191+
julia> static_step(static(1):static(3):9)
192+
static(3)
193+
194+
julia> static_step(1:3:9)
195+
3
196+
197+
julia> static_step(1:9)
198+
static(1)
199+
200+
```
201+
"""
159202
static_step(@nospecialize x::AbstractUnitRange) = StaticInt(1)
160203
static_step(x::OptionallyStaticStepRange) = getfield(x, :step)
161204
static_step(x) = step(x)
162205

206+
"""
207+
static_last(x::AbstractRange)
208+
209+
Attempt to return `static(last(x))`, if known at compile time. Otherwise, return
210+
`last(x)`.
211+
212+
See also: [`static_first`](@ref), [`static_step`](@ref)
213+
214+
# Examples
215+
216+
```julia
217+
julia> static_last(static(1):static(10))
218+
static(10)
219+
220+
julia> static_last(static(1):10)
221+
10
222+
223+
```
224+
"""
163225
static_last(x::OptionallyStaticRange) = getfield(x, :stop)
164226
static_last(x) = last(x)
165227
static_last(x::Union{Base.Slice, Base.IdentityUnitRange}) = static_last(x.indices)

0 commit comments

Comments
 (0)