File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed
tests/run/mirror-defaultArgument Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments