diff --git a/README.md b/README.md index e8a29e904..551eedc30 100644 --- a/README.md +++ b/README.md @@ -269,6 +269,8 @@ Currently, the `@compat` macro supports the following syntaxes: * `Dates.Period` rounding (e.g., `round(Dates.Hour(36), Dates.Day, RoundNearestTiesUp) == Dates.Day(2)` ([#24182]). +* `firstindex` to obtain the first index of an iterable ([#25458]). + ## Renaming diff --git a/src/Compat.jl b/src/Compat.jl index 05b4f3ae7..362fd8ffc 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -1546,6 +1546,14 @@ end @static if !isdefined(Base, :lastindex) const lastindex = endof export lastindex + firstindex(a::AbstractArray) = (Base.@_inline_meta; first(linearindices(a))) + firstindex(c::Char) = 1 + firstindex(c::Number) = 1 + firstindex(p::Pair) = 1 + firstindex(cmd::Cmd) = firstindex(cmd.exec) + firstindex(s::AbstractString) = 1 + firstindex(t::Tuple) = 1 + export firstindex end # 0.7.0-DEV.3585 diff --git a/test/runtests.jl b/test/runtests.jl index 431f73c19..afc5775ef 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1330,6 +1330,7 @@ end # 0.7.0-DEV.3583 @test lastindex(zeros(4)) == 4 @test lastindex(zeros(4,4)) == 16 +@test all(x -> firstindex(x) == 1, ([1, 2], [1 2; 3 4], 'a', 1, 1=>2, `foo`, "foo", (1, 2))) # 0.7.0-DEV.3585 let buf = IOBuffer()