-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14986 from dotty-staging/fix-12919
- Loading branch information
Showing
6 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
case class Normal(value: String) | ||
object Normal | ||
|
||
case class ClassWithCaseCompanion(value: String) | ||
case object ClassWithCaseCompanion | ||
|
||
def instantiate[T](product: Product)(implicit mirror: scala.deriving.Mirror.ProductOf[T]) = | ||
mirror.fromProduct(product) | ||
|
||
@main def Test: Unit = { | ||
assert(instantiate[Normal](Tuple1("a")) == Normal("a")) // works as expected | ||
|
||
assert(instantiate[ClassWithCaseCompanion.type](EmptyTuple) == ClassWithCaseCompanion) // works as expected | ||
|
||
val c = instantiate[ClassWithCaseCompanion](Tuple1("b")) // throws java.lang.ClassCastException: class ClassWithCaseCompanion$ cannot be cast to class ClassWithCaseCompanion | ||
assert(c == ClassWithCaseCompanion("b")) // desired behaviour | ||
|
||
val d = instantiate[ClassWithCaseCompanion.type](EmptyTuple) | ||
assert(d == ClassWithCaseCompanion) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import scala.deriving.Mirror | ||
|
||
case class Standalone(i: Int) | ||
object Standalone | ||
|
||
case class WithCompanionCaseClass(i: Int) | ||
case object WithCompanionCaseClass | ||
|
||
@main def Test: Unit = | ||
|
||
val mStandalone = summon[Mirror.ProductOf[Standalone]] | ||
assert(mStandalone eq Standalone) // the companion object is the mirror for the case class | ||
|
||
val mWithCompanion = summon[Mirror.ProductOf[WithCompanionCaseClass]] | ||
assert(mWithCompanion ne WithCompanionCaseClass) // A case object can not be the mirror of a companion case class. | ||
|
||
val mWithCompanionCaseObject = summon[Mirror.ProductOf[WithCompanionCaseClass.type]] | ||
assert(mWithCompanionCaseObject eq WithCompanionCaseClass) // A case object is its own mirror. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import scala.deriving.Mirror | ||
|
||
|
||
sealed trait WithCompanionSealedTrait | ||
case object WithCompanionSealedTrait: | ||
case class FirstChild(x: Int) extends WithCompanionSealedTrait | ||
|
||
@main def Test: Unit = | ||
|
||
val mWithCompanionSum = summon[Mirror.SumOf[WithCompanionSealedTrait]] | ||
assert(mWithCompanionSum.ordinal(WithCompanionSealedTrait.FirstChild(1)) == 0) | ||
assert(mWithCompanionSum ne WithCompanionSealedTrait) // A case object can not be the mirror of a companion case class. | ||
|
||
val mWithCompanionSingleton = summon[Mirror.ProductOf[WithCompanionSealedTrait.type]] | ||
assert(mWithCompanionSingleton.fromProduct(EmptyTuple) == WithCompanionSealedTrait) | ||
assert(mWithCompanionSingleton.isInstanceOf[Mirror.Singleton]) // case object is its own mirror. | ||
assert(mWithCompanionSingleton eq WithCompanionSealedTrait) // case object is its own mirror. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
trait Encoder[T] | ||
object Encoder: | ||
def derived[T](using scala.deriving.Mirror.Of[T]): Encoder[T] = new Encoder[T] {} | ||
|
||
case object Bar | ||
enum Bar derives Encoder: | ||
case A, B | ||
|
||
@main def Test: Unit = | ||
summon[Encoder[Bar]] |