Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modification via a type alias failing on Scala 3 #97

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,16 @@ object QuicklensMacros {
}

def termMethodByNameUnsafe(term: Term, name: String): Symbol = {
term.tpe.typeSymbol
term.tpe.widen.dealias.typeSymbol
.memberMethod(name)
.headOption
.getOrElse(report.errorAndAbort(noSuchMember(term, name)))
}

def termAccessorMethodByNameUnsafe(term: Term, name: String): (Symbol, Int) = {
val caseParamNames = term.tpe.typeSymbol.primaryConstructor.paramSymss.flatten.filter(_.isTerm).map(_.name)
val caseParamNames = term.tpe.widen.dealias.typeSymbol.primaryConstructor.paramSymss.flatten.filter(_.isTerm).map(_.name)
val idx = caseParamNames.indexOf(name)
term.tpe.typeSymbol.caseFields.find(_.name == name).getOrElse(report.errorAndAbort(noSuchMember(term, name)))
term.tpe.widen.dealias.typeSymbol.caseFields.find(_.name == name).getOrElse(report.errorAndAbort(noSuchMember(term, name)))
-> (idx + 1)
}

Expand All @@ -160,7 +160,7 @@ object QuicklensMacros {
obj: Term,
fields: Seq[(PathSymbol.Field, Seq[PathTree])]
): Term = {
val objSymbol = obj.tpe.typeSymbol
val objSymbol = obj.tpe.widen.dealias.typeSymbol
if objSymbol.flags.is(Flags.Case) then {
val copy = termMethodByNameUnsafe(obj, "copy")
val argsMap: Map[Int, Term] = fields.map { (field, trees) =>
Expand All @@ -172,15 +172,15 @@ object QuicklensMacros {
idx -> namedArg
}.toMap

val fieldsIdxs = 1.to(obj.tpe.typeSymbol.primaryConstructor.paramSymss.flatten.filter(_.isTerm).length)
val fieldsIdxs = 1.to(objSymbol.primaryConstructor.paramSymss.flatten.filter(_.isTerm).length)
val args = fieldsIdxs.map { i =>
argsMap.getOrElse(
i,
Select(obj, termMethodByNameUnsafe(obj, "copy$default$" + i.toString))
)
}.toList

obj.tpe.widen match {
obj.tpe.widen.dealias match {
// if the object's type is parametrised, we need to call .copy with the same type parameters
case AppliedType(_, typeParams) => Apply(TypeApply(Select(obj, copy), typeParams.map(Inferred(_))), args)
case _ => Apply(Select(obj, copy), args)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.softwaremill.quicklens

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import ModifyAliasTest._

object ModifyAliasTest {

case class State(x: Int)

type S = State
}
OndrejSpanel marked this conversation as resolved.
Show resolved Hide resolved

class ModifyAliasTest extends AnyFlatSpec with Matchers {
it should "modify an object declared using type alias" in {
val s: S = State(0)
val modified = s.modify(_.x).setTo(1)

modified.x shouldBe 1
}
}
OndrejSpanel marked this conversation as resolved.
Show resolved Hide resolved