Skip to content

Commit f5c1d08

Browse files
committed
materialized metadata is wrapped into additional container for lazy vals
1 parent 7731b93 commit f5c1d08

File tree

1 file changed

+15
-8
lines changed
  • commons-macros/src/main/scala/com/avsystem/commons/macros/rpc

1 file changed

+15
-8
lines changed

commons-macros/src/main/scala/com/avsystem/commons/macros/rpc/RpcMacros.scala

+15-8
Original file line numberDiff line numberDiff line change
@@ -136,29 +136,36 @@ class RpcMacros(ctx: blackbox.Context) extends RpcMacroCommons(ctx)
136136
}
137137

138138
val constructor = new RpcMetadataConstructor(metadataTpe)
139+
// separate object for cached implicits so that lazy vals are members instead of local variables
140+
val depsObj = c.freshName(TermName("deps"))
141+
val selfName = c.freshName(TermName("self"))
139142

140143
typedCompanionOf(metadataTpe) match {
141144
case Some(comp) =>
142145
// short circuit recursive implicit searches for M.Lazy[Real]
143146
val lazyMetadataTpe = getType(tq"$comp.Lazy[${realRpc.tpe}]")
144-
val selfName = c.freshName(TermName("self"))
145147
val lazySelfName = c.freshName(TermName("lazySelf"))
146148
registerImplicit(lazyMetadataTpe, lazySelfName)
147149
val tree = constructor.materializeFor(realRpc)
148150

149151
q"""
150-
var $selfName = null.asInstanceOf[$metadataTpe]
151-
val $lazySelfName = $comp.Lazy($selfName)
152-
..$cachedImplicitDeclarations
153-
$selfName = $tree
154-
$selfName
152+
object $depsObj {
153+
var $selfName: $metadataTpe = _
154+
private val $lazySelfName = $comp.Lazy($selfName)
155+
..$cachedImplicitDeclarations
156+
$selfName = $tree
157+
}
158+
$depsObj.$selfName
155159
"""
156160

157161
case None =>
158162
val tree = constructor.materializeFor(realRpc)
159163
q"""
160-
..$cachedImplicitDeclarations
161-
$tree
164+
object $depsObj {
165+
..$cachedImplicitDeclarations
166+
val $selfName = $tree
167+
}
168+
$depsObj.$selfName
162169
"""
163170
}
164171
}

0 commit comments

Comments
 (0)