|
1 | 1 |
|
2 | | -functor(T, x) = (), _ -> x |
| 2 | +functor(T, x) = (), Returns(x) |
3 | 3 | functor(x) = functor(typeof(x), x) |
4 | 4 |
|
5 | 5 | functor(::Type{<:Tuple}, x) = x, identity |
6 | | -functor(::Type{<:NamedTuple{L}}, x) where L = NamedTuple{L}(map(s -> getproperty(x, s), L)), identity |
| 6 | +functor(::Type{<:NamedTuple{L}}, x) where L = NamedTuple{L}(map(s -> getfield(x, s), L)), identity |
7 | 7 |
|
8 | 8 | functor(::Type{<:AbstractArray}, x) = x, identity |
9 | | -functor(::Type{<:AbstractArray{<:Number}}, x) = (), _ -> x |
| 9 | +functor(::Type{<:AbstractArray{<:Number}}, x) = (), Returns(x) |
10 | 10 |
|
11 | 11 | function makefunctor(m::Module, T, fs = fieldnames(T)) |
12 | 12 | yᵢ = 0 |
13 | 13 | escargs = map(fieldnames(T)) do f |
14 | 14 | f in fs ? :(y[$(yᵢ += 1)]) : :(x.$f) |
15 | 15 | end |
16 | | - escfs = [:($f=x.$f) for f in fs] |
| 16 | + escfs = [:($f = getfield(x, $(QuoteNode(f)))) for f in fs] |
17 | 17 |
|
18 | 18 | @eval m begin |
19 | 19 | $Functors.functor(::Type{<:$T}, x) = ($(escfs...),), y -> $T($(escargs...)) |
@@ -72,3 +72,20 @@ if VERSION < v"1.7" |
72 | 72 | # but for 1.6 this seems to work instead: |
73 | 73 | ismutabletype(@nospecialize t) = t.mutable |
74 | 74 | end |
| 75 | + |
| 76 | +# https://github.com/JuliaLang/julia/pull/39794 |
| 77 | +if VERSION < v"1.7.0-DEV.793" |
| 78 | + struct Returns{V} <: Function |
| 79 | + value::V |
| 80 | + Returns{V}(value) where {V} = new{V}(value) |
| 81 | + Returns(value) = new{Core.Typeof(value)}(value) |
| 82 | + end |
| 83 | + |
| 84 | + (obj::Returns)(args...; kw...) = obj.value |
| 85 | + function Base.show(io::IO, obj::Returns) |
| 86 | + show(io, typeof(obj)) |
| 87 | + print(io, "(") |
| 88 | + show(io, obj.value) |
| 89 | + print(io, ")") |
| 90 | + end |
| 91 | +end |
0 commit comments