Skip to content

Commit fe9235f

Browse files
authored
Alternative solution for wildcard type argument error
1 parent 32f3d5f commit fe9235f

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

docs/cross-build.md

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ It compiles with Scala 2, but Scala 3 produces the following errors:
7474

7575
~~~
7676
[error] -- [E043] Type Error:
77-
[error] 5 | def f(foo: Foo[_]): Unit // Warning with Scala 3
77+
[error] 5 | def f(foo: Foo[_]): Unit // Error with Scala 3
7878
[error] | ^^^^^^
7979
[error] |unreducible application of higher-kinded type Example.this.Foo to wildcard arguments
8080
[error] -- [E043] Type Error:
81-
[error] 6 | def g(foos: Seq[Foo[_]]): Unit // Warning with Scala 3
81+
[error] 6 | def g(foos: Seq[Foo[_]]): Unit // Error with Scala 3
8282
[error] | ^^^^^^
8383
[error] |unreducible application of higher-kinded type Example.this.Foo to wildcard arguments
8484
~~~
@@ -95,27 +95,17 @@ In the case of the function `f`, we can change its signature to take a type para
9595

9696
The second function, `g`, requires more work. We want to accept collections containing
9797
values of type `Foo[A]` with possibly different types for the parameter `A`. To achieve
98-
this, we create a wrapper type that models the type parameter as a type member instead:
98+
this, we create a wrapper class. The fact that the wrapper is a class and not an abstract
99+
type member makes it possible to apply a wildcard type argument to it:
99100

100101
~~~ scala
101-
// Wrapper type
102-
trait SomeFoo {
103-
type T
104-
def value: Foo[T]
105-
}
106-
107-
// Construct a value of type `SomeFoo`
108-
def SomeFoo[A](foo: Foo[A]): SomeFoo =
109-
new SomeFoo {
110-
type T = A
111-
def value = foo
112-
}
113-
114-
def g(foos: Seq[SomeFoo]): Unit // Compiles with both Scala 2 and Scala 3
102+
// Wrapper class
103+
class FooWrapper[A](val value: Foo[A])
104+
105+
def g(foos: Seq[FooWrapper[_]]): Unit // Compiles with both Scala 2 and Scala 3
115106
~~~
116107

117-
Users will have to explicitly wrap their `Foo` values into `SomeFoo` by calling the
118-
corresponding constructor.
108+
Users will have to explicitly wrap their `Foo` values into the `FooWrapper` class.
119109

120110
### Other incompatibilities
121111

0 commit comments

Comments
 (0)