Skip to content

Commit e3a642c

Browse files
authored
Backport ComposedFunction (#720)
1 parent a135ef3 commit e3a642c

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
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 = "3.16.0"
3+
version = "3.17.0"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ changes in `julia`.
5555

5656
## Supported features
5757

58+
* The composition operator `` now returns a `Compat.ComposedFunction` instead of an anonymous function ([#37517]). (since Compat 3.17)
59+
5860
* New function `addenv` for adding environment mappings into a `Cmd` object, returning the new `Cmd` object ([#37244]). (since Compat 3.16)
5961

6062
* `contains(haystack, needle)` and its one argument partially applied form `contains(haystack)` have been added, it acts like `occursin(needle, haystack)` ([#35132]). (since Compat 3.15)
@@ -200,3 +202,4 @@ Note that you should specify the correct minimum version for `Compat` in the
200202
[#35132]: https://github.com/JuliaLang/julia/pull/35132
201203
[#35052]: https://github.com/JuliaLang/julia/pull/35052
202204
[#37244]: https://github.com/JuliaLang/julia/pull/37244
205+
[#37517]: https://github.com/JuliaLang/julia/pull/37517

src/Compat.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,37 @@ if VERSION < v"1.5.0-DEV.438" # 0a43c0f1d21ce9c647c49111d93927369cd20f85
607607
Base.startswith(s) = Base.Fix2(startswith, s)
608608
end
609609

610+
# https://github.com/JuliaLang/julia/pull/37517
611+
if VERSION < v"1.6.0-DEV.1037"
612+
export ComposedFunction
613+
# https://github.com/JuliaLang/julia/pull/35980
614+
if VERSION < v"1.6.0-DEV.85"
615+
const ComposedFunction = let h = identity convert
616+
Base.typename(typeof(h)).wrapper
617+
end
618+
@eval ComposedFunction{F,G}(f, g) where {F,G} =
619+
$(Expr(:new, :(ComposedFunction{F,G}), :f, :g))
620+
ComposedFunction(f, g) = ComposedFunction{Core.Typeof(f),Core.Typeof(g)}(f, g)
621+
else
622+
using Base: ComposedFunction
623+
end
624+
function Base.getproperty(c::ComposedFunction, p::Symbol)
625+
if p === :f
626+
return getfield(c, :f)
627+
elseif p === :g
628+
return getfield(c, :g)
629+
elseif p === :outer
630+
return getfield(c, :f)
631+
elseif p === :inner
632+
return getfield(c, :g)
633+
end
634+
error("type ComposedFunction has no property ", p)
635+
end
636+
Base.propertynames(c::ComposedFunction) = (:f, :g, :outer, :inner)
637+
else
638+
using Base: ComposedFunction
639+
end
640+
610641
# https://github.com/JuliaLang/julia/pull/37244
611642
if VERSION < v"1.6.0-DEV.873" # 18198b1bf85125de6cec266eac404d31ccc2e65c
612643
export addenv

test/runtests.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,22 @@ end
556556
@test endswith("d")("abcd")
557557
end
558558

559+
# https://github.com/JuliaLang/julia/pull/37517
560+
@testset "ComposedFunction" begin
561+
@test sin cos isa Compat.ComposedFunction
562+
@test sin cos === Compat.ComposedFunction(sin, cos)
563+
c = sin cos
564+
@test c.outer === sin
565+
@test c.inner === cos
566+
if VERSION < v"1.6.0-DEV.1037"
567+
@test c.f === sin
568+
@test c.g === cos
569+
@test propertynames(c) == (:f, :g, :outer, :inner)
570+
else
571+
@test propertynames(c) == (:outer, :inner)
572+
end
573+
end
574+
559575
# From spawn.jl
560576
shcmd = `sh`
561577
havebb = false

0 commit comments

Comments
 (0)