Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
name = "Static"
uuid = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
authors = ["chriselrod", "ChrisRackauckas", "Tokazama"]
version = "1.2.0"
version = "1.3.0"

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

[compat]
Aqua = "0.8.4"
CommonWorldInvalidations = "1"
IfElse = "0.1"
PrecompileTools = "1.1"
SciMLPublic = "1.0.0"
Test = "1"
julia = "1.10"

Expand Down
12 changes: 9 additions & 3 deletions src/Static.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
module Static

import IfElse: ifelse
using SciMLPublic: @public

export StaticInt, StaticFloat64, StaticSymbol, True, False, StaticBool, NDIndex
export dynamic, is_static, known, static, static_promote
export dynamic, is_static, known, static, static_promote, static_first, static_step,
static_last

@public OptionallyStaticRange,
OptionallyStaticUnitRange, OptionallyStaticStepRange, SUnitRange, SOneTo
@public eachop, eachop_tuple, reduce_tup, eq, ne, gt, ge, le, lt, mul, add

import PrecompileTools: @recompile_invalidations

Expand Down Expand Up @@ -969,8 +975,8 @@ end
function Base.show(io::IO, @nospecialize(x::Union{StaticNumber, StaticSymbol, NDIndex}))
show(io, MIME"text/plain"(), x)
end
function Base.show(io::IO, ::MIME"text/plain",
@nospecialize(x::Union{StaticNumber, StaticSymbol}))
function Base.show(
io::IO, ::MIME"text/plain", @nospecialize(x::Union{StaticNumber, StaticSymbol}))
print(io, "static(" * repr(known(typeof(x))) * ")")
nothing
end
Expand Down
66 changes: 64 additions & 2 deletions src/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,81 @@ SOneTo(n::Int) = SOneTo{n}()
Base.oneto(::StaticInt{N}) where {N} = SOneTo{N}()

const OptionallyStaticRange{
F, L} = Union{OptionallyStaticUnitRange{F, L},
F, L} = Union{OptionallyStaticUnitRange{F, L},
OptionallyStaticStepRange{F, <:Any, L}}

# these probide a generic method for extracting potentially static values.
"""
static_first(x::AbstractRange)

Attempt to return `static(first(x))`, if known at compile time. Otherwise, return
`first(x)`.

See also: [`static_step`](@ref), [`static_last`](@ref)

# Examples

```julia
julia> static_first(static(2):10)
static(2)

julia> static_first(1:10)
1

julia> static_first(Base.OneTo(10))
static(1)

```
"""
static_first(x::Base.OneTo) = StaticInt(1)
static_first(x::Union{Base.Slice, Base.IdentityUnitRange}) = static_first(x.indices)
static_first(x::OptionallyStaticRange) = getfield(x, :start)
static_first(x) = first(x)

"""
static_step(x::AbstractRange)

Attempt to return `static(step(x))`, if known at compile time. Otherwise, return
`step(x)`.

See also: [`static_first`](@ref), [`static_last`](@ref)

# Examples

```julia
julia> static_step(static(1):static(3):9)
static(3)

julia> static_step(1:3:9)
3

julia> static_step(1:9)
static(1)

```
"""
static_step(@nospecialize x::AbstractUnitRange) = StaticInt(1)
static_step(x::OptionallyStaticStepRange) = getfield(x, :step)
static_step(x) = step(x)

"""
static_last(x::AbstractRange)

Attempt to return `static(last(x))`, if known at compile time. Otherwise, return
`last(x)`.

See also: [`static_first`](@ref), [`static_step`](@ref)

# Examples

```julia
julia> static_last(static(1):static(10))
static(10)

julia> static_last(static(1):10)
10

```
"""
static_last(x::OptionallyStaticRange) = getfield(x, :stop)
static_last(x) = last(x)
static_last(x::Union{Base.Slice, Base.IdentityUnitRange}) = static_last(x.indices)
Expand Down
Loading