You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cross-build.md
+65Lines changed: 65 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -52,6 +52,71 @@ implicit val locationWrites: Writes[Location] = (
52
52
)(location => (location.lat, location.long)) // Compiles with both Scala 2 and Scala 3
53
53
~~~
54
54
55
+
### Unreducible application of higher-kinded type to wildcard arguments
56
+
57
+
This error happens when you apply the “wildcard” type to a higher-kinded type.
58
+
59
+
Consider the following example:
60
+
61
+
~~~scala
62
+
traitExample {
63
+
64
+
typeFoo[A]
65
+
66
+
deff(foo: Foo[_]):Unit// Error with Scala 3
67
+
68
+
defg(foos: Seq[Foo[_]]):Unit// Error with Scala 3
69
+
70
+
}
71
+
~~~
72
+
73
+
It compiles with Scala 2, but Scala 3 produces the following errors:
74
+
75
+
~~~
76
+
[error] -- [E043] Type Error:
77
+
[error] 5 | def f(foo: Foo[_]): Unit // Warning with Scala 3
78
+
[error] | ^^^^^^
79
+
[error] |unreducible application of higher-kinded type Example.this.Foo to wildcard arguments
80
+
[error] -- [E043] Type Error:
81
+
[error] 6 | def g(foos: Seq[Foo[_]]): Unit // Warning with Scala 3
82
+
[error] | ^^^^^^
83
+
[error] |unreducible application of higher-kinded type Example.this.Foo to wildcard arguments
84
+
~~~
85
+
86
+
The reason for this error is that the type `Foo[_]` involves existential type quantification and this feature [has been removed from Scala 3](https://dotty.epfl.ch/docs/reference/dropped-features/existential-types.html).
87
+
88
+
Two solutions can be considered for cross-compiling.
89
+
90
+
In the case of the function `f`, we can change its signature to take a type parameter:
91
+
92
+
~~~scala
93
+
deff[A](foo: Foo[A]):Unit// Compiles with both Scala 2 and Scala 3
94
+
~~~
95
+
96
+
The second function, `g`, requires more work. We want to accept collections containing
97
+
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:
99
+
100
+
~~~scala
101
+
// Wrapper type
102
+
traitSomeFoo {
103
+
typeT
104
+
defvalue:Foo[T]
105
+
}
106
+
107
+
// Construct a value of type `SomeFoo`
108
+
defSomeFoo[A](foo: Foo[A]):SomeFoo=
109
+
newSomeFoo {
110
+
typeT=A
111
+
defvalue= foo
112
+
}
113
+
114
+
defg(foos: Seq[SomeFoo]):Unit// Compiles with both Scala 2 and Scala 3
115
+
~~~
116
+
117
+
Users will have to explicitly wrap their `Foo` values into `SomeFoo` by calling the
0 commit comments