Skip to content

Commit 75fef99

Browse files
authored
Change getproperty to getfield (#48)
* getproperty -> getfield * compat Returns
1 parent 977e357 commit 75fef99

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/functor.jl

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11

2-
functor(T, x) = (), _ -> x
2+
functor(T, x) = (), Returns(x)
33
functor(x) = functor(typeof(x), x)
44

55
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
77

88
functor(::Type{<:AbstractArray}, x) = x, identity
9-
functor(::Type{<:AbstractArray{<:Number}}, x) = (), _ -> x
9+
functor(::Type{<:AbstractArray{<:Number}}, x) = (), Returns(x)
1010

1111
function makefunctor(m::Module, T, fs = fieldnames(T))
1212
yᵢ = 0
1313
escargs = map(fieldnames(T)) do f
1414
f in fs ? :(y[$(yᵢ += 1)]) : :(x.$f)
1515
end
16-
escfs = [:($f=x.$f) for f in fs]
16+
escfs = [:($f = getfield(x, $(QuoteNode(f)))) for f in fs]
1717

1818
@eval m begin
1919
$Functors.functor(::Type{<:$T}, x) = ($(escfs...),), y -> $T($(escargs...))
@@ -72,3 +72,20 @@ if VERSION < v"1.7"
7272
# but for 1.6 this seems to work instead:
7373
ismutabletype(@nospecialize t) = t.mutable
7474
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

Comments
 (0)