@@ -7,29 +7,29 @@ eltypes(::Type{NamedTuple{K, V}}) where {K, V} = eltypes(V)
77
88Base. @pure SkipConstructor (:: Type ) = false
99
10- @generated function get_ith (s :: StructArray{T} , I ... ) where {T}
11- args = []
12- for key in fields (T)
13- field = Expr (:. , :s , Expr (:quote , key))
14- push! (args, :( $ field[I ... ] ))
10+ function foreach_expr (f, T, args ... )
11+ exprs = []
12+ for (ind, key) in enumerate ( fields (T) )
13+ new_args = ( Expr (:call , :getfieldindex , arg, Expr (:quote , key), ind) for arg in args )
14+ push! (exprs, f (new_args ... ))
1515 end
16+ exprs
17+ end
18+
19+ @generated function get_ith (s:: StructArray{T} , I... ) where {T}
20+ exprs = foreach_expr (field -> :($ field[I... ]), T, :s )
1621 return quote
1722 @boundscheck checkbounds (s, I... )
18- @inbounds $ (Expr (:call , :createinstance , :T , args ... ))
23+ @inbounds $ (Expr (:call , :createinstance , :T , exprs ... ))
1924 end
2025end
2126
2227@generated function set_ith! (s:: StructArray{T} , vals, I... ) where {T}
23- args = []
24- for key in fields (T)
25- field = Expr (:., :s , Expr (:quote , key))
26- val = Expr (:., :vals , Expr (:quote , key))
27- push! (args, :($ field[I... ] = $ val))
28- end
29- push! (args, :s )
28+ exprs = foreach_expr ((field, val) -> :($ field[I... ] = $ val), T, :s , :vals )
29+ push! (exprs, :s )
3030 return quote
3131 @boundscheck checkbounds (s, I... )
32- @inbounds $ (Expr (:block , args ... ))
32+ @inbounds $ (Expr (:block , exprs ... ))
3333 end
3434end
3535
0 commit comments