Skip to content

Commit

Permalink
Merge pull request #80 from KacperFKorban/abstract-classes
Browse files Browse the repository at this point in the history
Fix supporting modifying `sealed abstract classes` in Scala 3
  • Loading branch information
adamw authored Feb 14, 2022
2 parents 66f4bba + ffd23ae commit 598e730
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,10 @@ object QuicklensMacros {
case AppliedType(_, typeParams) => Apply(TypeApply(Select(obj, copy), typeParams.map(Inferred(_))), args)
case _ => Apply(Select(obj, copy), args)
}
else if (objSymbol.flags.is(Flags.Sealed) && objSymbol.flags.is(Flags.Trait)) || objSymbol.flags.is(
Flags.Enum
)
else if objSymbol.flags.is(Flags.Enum) ||
(objSymbol.flags.is(Flags.Sealed) && (objSymbol.flags.is(Flags.Trait) || objSymbol.flags.is(Flags.Abstract)))
then
// if the source is a sealed trait / enum, generating a if-then-else with a .copy for each child (implementing case class)
// if the source is a sealed trait / sealed abstract class / enum, generating a if-then-else with a .copy for each child (implementing case class)
val cases = obj.tpe.typeSymbol.children.map { child =>
val subtype = TypeIdent(child)
val bind = Symbol.newBind(owner, "c", Flags.EmptyFlags, subtype.tpe)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.softwaremill.quicklens.test

import com.softwaremill.quicklens.TestData._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import com.softwaremill.quicklens._

class ModitySealedAbstractClass extends AnyFlatSpec with Matchers {
it should "Modify abstract class hierarchy" in {
invInt.modify(_.typ).setTo(Type("Long")) should be(invLong)
}
}
11 changes: 11 additions & 0 deletions quicklens/src/test/scala/com/softwaremill/quicklens/TestData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,15 @@ object TestData {
val ms1 = collection.immutable.SortedMap("K1" -> A4(A5("d1")), "K2" -> A4(A5("d2")), "K3" -> A4(A5("d3")))
val mh1 = collection.immutable.HashMap("K1" -> A4(A5("d1")), "K2" -> A4(A5("d2")), "K3" -> A4(A5("d3")))
val ml1 = collection.immutable.ListMap("K1" -> A4(A5("d1")), "K2" -> A4(A5("d2")), "K3" -> A4(A5("d3")))

case class Type(name: String)
sealed abstract class Variance {
val typ: Type
}
case class Covariance(typ: Type) extends Variance
case class Contravariance(typ: Type) extends Variance
case class Invariance(typ: Type) extends Variance

val invInt: Variance = Invariance(Type("Int"))
val invLong: Variance = Invariance(Type("Long"))
}

0 comments on commit 598e730

Please sign in to comment.