Skip to content

Commit 815836c

Browse files
authored
Merge pull request #83 from JuliaDiff/ox/nongenbacking
Bring back nongenerated backing
2 parents 9a56965 + 5fac768 commit 815836c

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/compat.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
11
if VERSION < v"1.2"
22
Base.getproperty(x::Tuple, f::Int) = getfield(x, f)
33
end
4+
5+
if VERSION < v"1.1"
6+
# Note: these are actually *better* than the ones in julia 1.1, 1.2, 1.3,and 1.4
7+
# See: https://github.com/JuliaLang/julia/issues/34292
8+
function fieldtypes(::Type{T}) where T
9+
if @generated
10+
ntuple(i -> fieldtype(T, i), fieldcount(T))
11+
else
12+
ntuple(i -> fieldtype(T, i), fieldcount(T))
13+
end
14+
end
15+
16+
function fieldnames(::Type{T}) where T
17+
if @generated
18+
ntuple(i -> fieldname(T, i), fieldcount(T))
19+
else
20+
ntuple(i -> fieldname(T, i), fieldcount(T))
21+
end
22+
end
23+
end

src/differentials/composite.jl

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,27 @@ 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::T)::NamedTuple where T
81+
# note: all computation outside the if @generated happens at runtime.
82+
# so the first 4 lines of the branchs look the same, but can not be moved out.
83+
# see https://github.com/JuliaLang/julia/issues/34283
84+
if @generated
85+
!isstructtype(T) && throw(DomainError(T, "backing can only be use on composite types"))
86+
nfields = fieldcount(T)
87+
names = fieldnames(T)
88+
types = fieldtypes(T)
89+
90+
vals = Expr(:tuple, ntuple(ii->:(getfield(x, $ii)), nfields)...)
91+
return :(NamedTuple{$names, Tuple{$(types...)}}($vals))
92+
else
93+
!isstructtype(T) && throw(DomainError(T, "backing can only be use on composite types"))
94+
nfields = fieldcount(T)
95+
names = fieldnames(T)
96+
types = fieldtypes(T)
97+
98+
vals = ntuple(ii->getfield(x, ii), nfields)
99+
return NamedTuple{names, Tuple{types...}}(vals)
100+
end
88101
end
89102

90103
"""

0 commit comments

Comments
 (0)