From f395bd9708c88022e35ef0f0ec2d3153c1941b0b Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Tue, 14 Nov 2023 07:52:32 -0800 Subject: [PATCH 1/3] FAQ for type inference --- _overviews/FAQ/index.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/_overviews/FAQ/index.md b/_overviews/FAQ/index.md index 0d6cb2eea7..75fa976757 100644 --- a/_overviews/FAQ/index.md +++ b/_overviews/FAQ/index.md @@ -196,6 +196,19 @@ be written using the `_` syntax. See also [SLS 6.23.2](https://scala-lang.org/files/archive/spec/2.13/06-expressions.html#placeholder-syntax-for-anonymous-functions). +### Why doesn't my dotted expression work? + +When methods have type parameters, the compiler makes a best effort to infer them +if they are not supplied explicitly in a method application. +Type parameter inference is improved in Scala 3. + +``` +scala> List("ab").toSet.head.head + ^ + error: value head is not a member of type parameter B +``` +For more context, see [this section of the tour](https://docs.scala-lang.org/tour/multiple-parameter-lists.html#drive-type-inference). + ### Can I chain or nest implicit conversions? Not really, but you can [make it work](https://stackoverflow.com/a/5332804). From 096fec671a1bd9ec24e71f07e2263219375648c9 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Tue, 28 Nov 2023 09:42:38 -0800 Subject: [PATCH 2/3] Adapt to Seth text --- _overviews/FAQ/index.md | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/_overviews/FAQ/index.md b/_overviews/FAQ/index.md index 75fa976757..0fe94cb634 100644 --- a/_overviews/FAQ/index.md +++ b/_overviews/FAQ/index.md @@ -196,18 +196,21 @@ be written using the `_` syntax. See also [SLS 6.23.2](https://scala-lang.org/files/archive/spec/2.13/06-expressions.html#placeholder-syntax-for-anonymous-functions). -### Why doesn't my dotted expression work? +### Why couldn't Scala infer the correct type in my code? -When methods have type parameters, the compiler makes a best effort to infer them -if they are not supplied explicitly in a method application. -Type parameter inference is improved in Scala 3. +It is difficult to generalize about type inference, because various features of the language +affect how your code is construed. There may be several ways to rewrite your code to make +the types fall out naturally. -``` -scala> List("ab").toSet.head.head - ^ - error: value head is not a member of type parameter B -``` -For more context, see [this section of the tour](https://docs.scala-lang.org/tour/multiple-parameter-lists.html#drive-type-inference). +The most straightforward workaround is to supply explicit types in your code. + +That may involve specifying an explicit type to a definition, or a type argument to a method. + +Type inference is greatly improved in Scala 3. If Scala 2 doesn't compile your code, it's worth trying with Scala 3. + +Sometimes, using multiple parameter lists helps inference, as explained in [this section of the language tour](https://docs.scala-lang.org/tour/multiple-parameter-lists.html#drive-type-inference). + +For paradigmatic questions about type inference involving `toSet`, see the discussions on [this ticket](https://github.com/scala/bug/issues/7743) and a related [Q&A](https://stackoverflow.com/questions/5544536/in-scala-2-type-inference-fails-on-set-made-with-toset). ### Can I chain or nest implicit conversions? From dda9ed598338db45effad20019d806bb0006c3b8 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Tue, 28 Nov 2023 10:11:04 -0800 Subject: [PATCH 3/3] Adapt to Seth review --- _overviews/FAQ/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_overviews/FAQ/index.md b/_overviews/FAQ/index.md index 0fe94cb634..96c5edd438 100644 --- a/_overviews/FAQ/index.md +++ b/_overviews/FAQ/index.md @@ -210,7 +210,7 @@ Type inference is greatly improved in Scala 3. If Scala 2 doesn't compile your c Sometimes, using multiple parameter lists helps inference, as explained in [this section of the language tour](https://docs.scala-lang.org/tour/multiple-parameter-lists.html#drive-type-inference). -For paradigmatic questions about type inference involving `toSet`, see the discussions on [this ticket](https://github.com/scala/bug/issues/7743) and a related [Q&A](https://stackoverflow.com/questions/5544536/in-scala-2-type-inference-fails-on-set-made-with-toset). +For common questions about type inference involving `toSet`, see the discussions on [this ticket](https://github.com/scala/bug/issues/7743) and a related [Q&A](https://stackoverflow.com/questions/5544536/in-scala-2-type-inference-fails-on-set-made-with-toset). ### Can I chain or nest implicit conversions?