Skip to content

Commit 29d692a

Browse files
committed
capture return-value in submodel
1 parent ecf1e5d commit 29d692a

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/submodel_macro.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,17 +212,21 @@ function submodel(prefix_expr, expr, ctx=esc(:__context__))
212212
if prefix_left !== :prefix
213213
error("$(prefix_left) is not a valid kwarg")
214214
end
215+
216+
# The user expects `@submodel ...` to return the
217+
# return-value of the `...`, hence we need to capture
218+
# the return-value and handle it correctly.
219+
@gensym retval
220+
215221
# `prefix=false` => don't prefix, i.e. do nothing to `ctx`.
216222
# `prefix=true` => automatically determine prefix.
217223
# `prefix=...` => use it.
218224
args_assign = getargs_assignment(expr)
219225
return if args_assign === nothing
220226
ctx = prefix_submodel_context(prefix, ctx)
221-
# In this case we only want to get the `__varinfo__`.
222227
quote
223-
$(esc(:__varinfo__)) = last(
224-
$(DynamicPPL._evaluate!!)($(esc(expr)), $(esc(:__varinfo__)), $(ctx))
225-
)
228+
$retval, $(esc(:__varinfo__)) = $(DynamicPPL._evaluate!!)($(esc(expr)), $(esc(:__varinfo__)), $(ctx))
229+
$retval
226230
end
227231
else
228232
L, R = args_assign
@@ -235,9 +239,10 @@ function submodel(prefix_expr, expr, ctx=esc(:__context__))
235239
)
236240
end
237241
quote
238-
$(esc(L)), $(esc(:__varinfo__)) = $(DynamicPPL._evaluate!!)(
242+
$retval, $(esc(:__varinfo__)) = $(DynamicPPL._evaluate!!)(
239243
$(esc(R)), $(esc(:__varinfo__)), $(ctx)
240244
)
245+
esc(L) = $retval
241246
end
242247
end
243248
end

0 commit comments

Comments
 (0)