Skip to content

Commit

Permalink
FAQ for type inference (#2924)
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt authored Nov 28, 2023
1 parent a7f7792 commit 3ea84c7
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions _overviews/FAQ/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,22 @@ 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 couldn't Scala infer the correct type in my code?

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.

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 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?

Not really, but you can [make it work](https://stackoverflow.com/a/5332804).
Expand Down

0 comments on commit 3ea84c7

Please sign in to comment.