Skip to content

Commit 9eeab91

Browse files
Add splat (#786)
1 parent 5e16dfb commit 9eeab91

File tree

4 files changed

+38
-44
lines changed

4 files changed

+38
-44
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Compat"
22
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
3-
version = "4.5.0"
3+
version = "4.6.0"
44

55
[deps]
66
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ changes in `julia`.
7070

7171
## Supported features
7272

73-
* `Splat(f)` which is equivalent to `args -> f(args...)`. ([#42717]) (since Compat 4.5.0)
73+
* `splat(f)` which is equivalent to `args -> f(args...)`. ([#42717], [#48038]) (since Compat 4.6.0) (Note: for historical reasons, `Compat` on Julia before v1.9 also exports `Splat`; its usage is discouraged, however.)
7474

7575
* `Compat.@assume_effects setting... ex` overrides the compiler's effect modeling for the method definition `ex` on Julia versions that support this feature. Julia version without support just pass back `ex`. ([#43852]) (since Compat 4.4.0)
7676

@@ -142,6 +142,7 @@ is at least this version by `VERSION >= v"X.Y.Z-aaa+NNNN"`.
142142
Note that you should specify the correct minimum version for `Compat` in the
143143
`[compat]` section of your `Project.toml`, as given in above list.
144144

145+
[`@bdf9ead9`]: https://github.com/JuliaLang/julia/commit/bdf9ead91e5a8dfd91643a17c1626032faada329
145146
[#29901]: https://github.com/JuliaLang/julia/issues/29901
146147
[#35316]: https://github.com/JuliaLang/julia/issues/35316
147148
[#36229]: https://github.com/JuliaLang/julia/issues/36229
@@ -157,6 +158,8 @@ Note that you should specify the correct minimum version for `Compat` in the
157158
[#41312]: https://github.com/JuliaLang/julia/issues/41312
158159
[#42125]: https://github.com/JuliaLang/julia/issues/42125
159160
[#42351]: https://github.com/JuliaLang/julia/issues/42351
160-
[#43354]: https://github.com/JuliaLang/julia/issues/43354
161+
[#42717]: https://github.com/JuliaLang/julia/issues/42717
161162
[#43334]: https://github.com/JuliaLang/julia/issues/43334
162-
[`@bdf9ead9`]: https://github.com/JuliaLang/julia/commit/bdf9ead91e5a8dfd91643a17c1626032faada329
163+
[#43354]: https://github.com/JuliaLang/julia/issues/43354
164+
[#43852]: https://github.com/JuliaLang/julia/issues/43852
165+
[#48038]: https://github.com/JuliaLang/julia/issues/48038

src/Compat.jl

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -616,44 +616,27 @@ if VERSION < v"1.9.0-DEV.1163"
616616

617617
_empty_stack(_...) = throw(ArgumentError("`stack` on an empty collection is not allowed"))
618618
end
619-
620-
@static if VERSION < v"1.9.0-DEV.513"
621-
# https://github.com/JuliaLang/julia/pull/42717
622-
export Splat
623-
624-
struct Splat{F} <: Function
625-
f::F
626-
Splat(f) = new{Core.Typeof(f)}(f)
619+
620+
if v"1.10.0-" <= VERSION < v"1.10.0-DEV.360" || VERSION < v"1.9.0-beta3"
621+
if VERSION < v"1.9.0-DEV.513"
622+
# https://github.com/JuliaLang/julia/pull/42717
623+
export Splat # Base does not export this, but we have to keep it for compatibility
624+
625+
struct Splat{F} <: Function
626+
f::F
627+
Splat(f) = new{Core.Typeof(f)}(f)
628+
end
629+
630+
(s::Splat)(args) = s.f(args...)
631+
Base.print(io::IO, s::Splat) = print(io, "splat(", s.f, ')')
632+
Base.show(io::IO, s::Splat) = print(io, s)
633+
Base.show(io::IO, ::MIME"text/plain", s::Splat) = show(io, s)
627634
end
628635

629-
(s::Splat)(args) = s.f(args...)
630-
Base.print(io::IO, s::Splat) = print(io, "Splat(", s.f, ')')
631-
Base.show(io::IO, s::Splat) = print(io, s)
632-
Base.show(io::IO, ::MIME"text/plain", s::Splat) = show(io, s)
633-
@doc """
634-
Splat(f)
635-
Equivalent to
636-
```julia
637-
my_splat(f) = args->f(args...)
638-
```
639-
i.e. given a function returns a new function that takes one argument and splats
640-
its argument into the original function. This is useful as an adaptor to pass
641-
a multi-argument function in a context that expects a single argument, but
642-
passes a tuple as that single argument. Additionally has pretty printing.
643-
# Example usage:
644-
```jldoctest
645-
julia> map(Base.Splat(+), zip(1:3,4:6))
646-
3-element Vector{Int64}:
647-
5
648-
7
649-
9
650-
julia> my_add = Base.Splat(+)
651-
Splat(+)
652-
julia> my_add((1,2,3))
653-
6
654-
```
655-
""" Splat
656-
end
636+
# https://github.com/JuliaLang/julia/pull/48038
637+
export splat
638+
splat(f) = Splat(f)
639+
end
657640

658641
include("deprecated.jl")
659642

test/runtests.jl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,16 @@ end
578578
end
579579
end
580580

581-
@testset "Splat" begin
582-
@test Splat(+)((1,2,3)) == 6
583-
@test repr(Splat(+)) == "Splat(+)"
584-
@test repr(MIME"text/plain"(), Splat(+)) == "Splat(+)"
581+
@testset "splat" begin
582+
@test splat(+)((1,2,3)) == 6
583+
584+
if v"1.10.0-" <= VERSION < v"1.10.0-DEV.360" ||
585+
v"1.9.0-DEV.513" <= VERSION < v"1.9.0-beta3"
586+
# these versions of Base export Splat (which we use) but pretty-print with capital `S`
587+
@test repr(splat(+)) == "Splat(+)"
588+
@test repr(MIME"text/plain"(), splat(+)) == "Splat(+)"
589+
else
590+
@test repr(splat(+)) == "splat(+)"
591+
@test repr(MIME"text/plain"(), splat(+)) == "splat(+)"
592+
end
585593
end

0 commit comments

Comments
 (0)