Skip to content

Commit a1d6bc2

Browse files
add test to check override of defaultArgument (when and only when needed)
1 parent a459960 commit a1d6bc2

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import scala.deriving._
2+
import scala.annotation.experimental
3+
import scala.quoted._
4+
5+
object MirrorOps:
6+
7+
inline def overridesDefaultArgument[T]: Boolean = ${ overridesDefaultArgumentImpl[T] }
8+
9+
def overridesDefaultArgumentImpl[T](using Quotes, Type[T]): Expr[Boolean] =
10+
import quotes.reflect.*
11+
val cls = TypeRepr.of[T].classSymbol.get
12+
val companion = cls.companionModule.moduleClass
13+
val methods = companion.declaredMethods
14+
15+
val experAnnotType = Symbol.requiredClass("scala.annotation.experimental").typeRef
16+
17+
Expr {
18+
methods.exists { m =>
19+
m.name == "defaultArgument" &&
20+
m.flags.is(Flags.Synthetic) &&
21+
m.annotations.exists(_.tpe <:< experAnnotType)
22+
}
23+
}
24+
25+
end MirrorOps
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import scala.deriving._
2+
import scala.annotation.experimental
3+
import scala.quoted._
4+
5+
import MirrorOps.*
6+
7+
object Test extends App:
8+
9+
case class WithoutDefault(x: Int)
10+
assert(!overridesDefaultArgument[WithoutDefault])
11+
12+
13+
case class WithDefault(x: Int, y: Int = 1)
14+
assert(overridesDefaultArgument[WithDefault])
15+
16+
val m = summon[Mirror.Of[WithDefault]]
17+
assert(m.defaultArgument(1) == 1)
18+
19+
try
20+
m.defaultArgument(0)
21+
throw IllegalStateException("There should be no default argument")
22+
catch
23+
case ex: NoSuchElementException => assert(ex.getMessage == "0") // Ok

0 commit comments

Comments
 (0)