Skip to content

Commit 3e58b5b

Browse files
authored
Bring back nongenerated backing
1 parent 9a56965 commit 3e58b5b

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

src/differentials/composite.jl

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,38 @@ backing(x::Tuple) = x
7777
backing(x::NamedTuple) = x
7878
backing(x::Composite) = getfield(x, :backing)
7979

80-
@generated function backing(x)::NamedTuple
81-
!isstructtype(x) && throw(DomainError(x, "backing can only be use on composite types"))
82-
nfields = fieldcount(x)
83-
names = ntuple(ii->fieldname(x, ii), nfields)
84-
types = ntuple(ii->fieldtype(x, ii), nfields)
85-
86-
vals = Expr(:tuple, ntuple(ii->:(getfield(x, $ii)), nfields)...)
87-
return :(NamedTuple{$names, Tuple{$(types...)}}($vals))
80+
function backing(x)::NamedTuple
81+
82+
83+
else
84+
!isstructtype(x) && throw(DomainError(x, "backing can only be use on composite types"))
85+
nfields = fieldcount(x)
86+
names = ntuple(ii->fieldname(x, ii), nfields)
87+
types = ntuple(ii->fieldtype(x, ii), nfields)
88+
89+
end
90+
91+
function backing(x::T)::NamedTuple where T
92+
# note: all computation outside the if @generated happens at runtime.
93+
# so the first 4 lines of the branchs look the same, but can not be moved out.
94+
# see https://github.com/JuliaLang/julia/issues/34283
95+
if @generated
96+
!isstructtype(T) && throw(DomainError(T, "backing can only be use on composite types"))
97+
nfields = fieldcount(T)
98+
names = ntuple(ii->fieldname(T, ii), nfields)
99+
types = ntuple(ii->fieldtype(T, ii), nfields)
100+
101+
vals = Expr(:tuple, ntuple(ii->:(getfield(x, $ii)), nfields)...)
102+
return :(NamedTuple{$names, Tuple{$(types...)}}($vals))
103+
else
104+
!isstructtype(T) && throw(DomainError(T, "backing can only be use on composite types"))
105+
nfields = fieldcount(T)
106+
names = ntuple(ii->fieldname(T, ii), nfields)
107+
types = ntuple(ii->fieldtype(T, ii), nfields)
108+
109+
vals = ntuple(ii->getfield(x, ii), nfields)
110+
return NamedTuple{names, Tuple{types...}}(vals)
111+
end
88112
end
89113

90114
"""

0 commit comments

Comments
 (0)