Skip to content

Commit 3836573

Browse files
committed
allowed vals and vars in MacroInstances
1 parent fca6351 commit 3836573

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

commons-core/src/test/scala/com/avsystem/commons/meta/MacroInstancesTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ object DependencyImplicits {
1111
}
1212

1313
trait ComplexInstances[T] {
14-
def plainCodec: GenCodec[Klass[Int]]
15-
def codecWithGeneric: GenCodec[Klass[T]]
14+
val plainCodec: GenCodec[Klass[Int]]
15+
var codecWithGeneric: GenCodec[Klass[T]]
1616
def dependencyUsingCodec: GenCodec[Klass[Dep]]
1717
def parameterizedCodec[A: GenCodec]: GenCodec[Klass[A]]
1818
}

commons-macros/src/main/scala/com/avsystem/commons/macros/misc/MiscMacros.scala

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ class MiscMacros(ctx: blackbox.Context) extends AbstractMacroCommons(ctx) {
355355
}
356356

357357
val instancesMethods = instancesTpe.members.iterator
358-
.filter(m => m.isAbstract && m.isMethod).map(_.asMethod).toList.reverse
358+
.filter(m => m.isAbstract && m.isMethod && !m.asTerm.isSetter).map(_.asMethod).toList.reverse
359359

360360
def impl(singleMethod: Option[Symbol]): Tree = {
361361
val impls = instancesMethods.map { m =>
@@ -368,9 +368,16 @@ class MiscMacros(ctx: blackbox.Context) extends AbstractMacroCommons(ctx) {
368368
if (singleMethod.exists(_ != m)) q"$PredefObj.???"
369369
else q"$resultCompanion.materialize"
370370

371-
val tparamDefs = sig.typeParams.map(typeSymbolToTypeDef(_, forMethod = true))
372-
val paramDefs = sig.paramLists.map(_.map(paramSymbolToValDef))
373-
q"def ${m.name}[..$tparamDefs](...$paramDefs): ${treeForType(sig.finalResultType)} = $body"
371+
val instTpeTree = treeForType(sig.finalResultType)
372+
if (!m.isGetter) {
373+
val tparamDefs = sig.typeParams.map(typeSymbolToTypeDef(_, forMethod = true))
374+
val paramDefs = sig.paramLists.map(_.map(paramSymbolToValDef))
375+
q"def ${m.name}[..$tparamDefs](...$paramDefs): $instTpeTree = $body"
376+
}
377+
else if (m.asTerm.isVar)
378+
q"var ${m.name}: $instTpeTree = $body"
379+
else
380+
q"val ${m.name}: $instTpeTree = $body"
374381
}
375382

376383
val implicitsName = c.freshName(TermName("implicits"))

0 commit comments

Comments
 (0)