Skip to content

Alternative solution for wildcard type argument error #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions docs/cross-build.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ It compiles with Scala 2, but Scala 3 produces the following errors:

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

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

~~~ scala
// Wrapper type
trait SomeFoo {
type T
def value: Foo[T]
}

// Construct a value of type `SomeFoo`
def SomeFoo[A](foo: Foo[A]): SomeFoo =
new SomeFoo {
type T = A
def value = foo
}

def g(foos: Seq[SomeFoo]): Unit // Compiles with both Scala 2 and Scala 3
// Wrapper class
class FooWrapper[A](val value: Foo[A])

def g(foos: Seq[FooWrapper[_]]): Unit // Compiles with both Scala 2 and Scala 3
~~~

Users will have to explicitly wrap their `Foo` values into `SomeFoo` by calling the
corresponding constructor.
Users will have to explicitly wrap their `Foo` values into the `FooWrapper` class.

### Other incompatibilities

Expand Down