|
28 | 28 | const _getproperty = getproperty |
29 | 29 | end |
30 | 30 |
|
31 | | -function _foreachfield(names, xs) |
| 31 | +function _foreachfield(names, L) |
| 32 | + vars = ntuple(i -> gensym(), L) |
32 | 33 | exprs = Expr[] |
| 34 | + for (i, v) in enumerate(vars) |
| 35 | + push!(exprs, Expr(:(=), v, Expr(:call, :getfield, :xs, i))) |
| 36 | + end |
33 | 37 | for field in names |
34 | 38 | sym = QuoteNode(field) |
35 | | - args = [Expr(:call, :_getproperty, :(getfield(xs, $j)), sym) for j in 1:length(xs)] |
| 39 | + args = [Expr(:call, :_getproperty, var, sym) for var in vars] |
36 | 40 | push!(exprs, Expr(:call, :f, args...)) |
37 | 41 | end |
38 | 42 | push!(exprs, :(return nothing)) |
39 | 43 | return Expr(:block, exprs...) |
40 | 44 | end |
41 | 45 |
|
42 | | -@generated foreachfield(::Type{<:NamedTuple{names}}, f, xs...) where {names} = _foreachfield(names, xs) |
43 | | -@generated foreachfield(::Type{<:NTuple{N, Any}}, f, xs...) where {N} = _foreachfield(Base.OneTo(N), xs) |
| 46 | +@generated foreachfield(::Type{<:NamedTuple{names}}, f, xs::Vararg{Any, L}) where {names, L} = |
| 47 | + _foreachfield(names, L) |
| 48 | +@generated foreachfield(::Type{<:NTuple{N, Any}}, f, xs::Vararg{Any, L}) where {N, L} = |
| 49 | + _foreachfield(Base.OneTo(N), L) |
44 | 50 |
|
45 | 51 | foreachfield(f, x::T, xs...) where {T} = foreachfield(staticschema(T), f, x, xs...) |
46 | 52 |
|
|
0 commit comments