Skip to content

Commit 9f27137

Browse files
committed
surface (fix): Fixes #3353 in building surfaces of classes with type params in Scala3
1 parent fb4de29 commit 9f27137

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

airframe-surface/src/main/scala-3/wvlet/airframe/surface/CompileTimeSurfaceFactory.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,10 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q):
584584
// https://github.com/lampepfl/dotty-macro-examples/blob/aed51833db652f67741089721765ad5a349f7383/defaultParamsInference/src/macro.scala
585585
val defaultValue: Expr[Option[Any]] = field.defaultValueGetter match
586586
case Some(m) =>
587-
val dv = Ref(m.owner.companionModule).select(m)
587+
val companion = Ref(t.typeSymbol.companionModule)
588+
// Populate method type parameters with Any type
589+
val dummyTypeList: List[TypeRepr] = m.paramSymss.flatten.map { tp => TypeRepr.of[Any] }.toList
590+
val dv: Term = companion.select(m).appliedToTypes(dummyTypeList)
588591
'{ Some(${ dv.asExprOf[Any] }) }
589592
case _ => '{ None }
590593

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package wvlet.airframe.surface
15+
16+
import wvlet.airspec.AirSpec
17+
18+
/**
19+
* Test generic types with default values
20+
*/
21+
object i3353 extends AirSpec {
22+
case class FValue[V](value: Option[Int] = None)
23+
24+
test("With an optional value default argument") {
25+
Surface.of[FValue[Int]]
26+
}
27+
28+
case class MValue[T](value: Seq[T] = Seq.empty)
29+
30+
test("With a default value of a Seq type") {
31+
Surface.of[MValue[Int]]
32+
}
33+
34+
case class NValue[T >: Null](value: T = null)
35+
36+
test("With a default value of a generic type") {
37+
Surface.of[NValue[String]]
38+
}
39+
40+
case class BValue[T](value: T, boolean: Boolean = false)
41+
42+
test("With a default value of a generic type and regular arg") {
43+
Surface.of[BValue[String]]
44+
}
45+
46+
case class ZValue[T](boolean: Boolean = false)
47+
48+
test("With a default value of a boolean type") {
49+
Surface.of[ZValue[String]]
50+
}
51+
}

0 commit comments

Comments
 (0)