Skip to content
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

FAQ for type inference #2924

Merged
merged 3 commits into from
Nov 28, 2023
Merged

FAQ for type inference #2924

merged 3 commits into from
Nov 28, 2023

Conversation

som-snytt
Copy link
Contributor

@som-snytt som-snytt commented Nov 14, 2023

Documents scala/bug#7743

The tour still needs tweaking. per #2648

@SethTisue SethTisue self-assigned this Nov 20, 2023
@SethTisue
Copy link
Member

SethTisue commented Nov 28, 2023

@som-snytt wdyt about the following version?

Why couldn't Scala infer the correct type for my code?

This is difficult to generalize about, as there could be a variety of causes and possible solutions.

Type inference was greatly improved in Scala 3. If you're using Scala 2, you might try your example in Scala 3 to see if it's handled any better.

The most common workaround is simply to add an explicit type to your code.

Sometimes using multiple parameter lists helps, as per this language tour section.

Questions about type inference often involve toSet. For discussion of examples involving toSet, see:

These discussions may shed light on other examples as well.

@SethTisue
Copy link
Member

@BalmungSan this might interest you as well

@SethTisue SethTisue marked this pull request as draft November 28, 2023 01:02
@som-snytt
Copy link
Contributor Author

If it's called dotty, then why doesn't my dotted expression work?

If I'm implying the correct type for my code, then why can't Scala infer it?

Are you using "imply" and "infer" correctly?

Expectation or Anticipation

I see my PR was more of a placeholder.

The github states are draft (WIP, drafts cannot be merged) and open green (ready for review), so if you're reviewing, it should be green. I don't know why there isn't a third state for "shared draft" that precedes green. That would mean, even if somebody accidentally grants an accepting review, don't merge it, as we're still mulling.

@BalmungSan
Copy link
Contributor

Personally, I really like this FAQ addition.

My proposal for the title may be:

Why couldn't Scala infer the correct type for my dot-chain expression?

Additionally, I think providing a concrete example of the suggested solutions would be useful, as well as explaining why that fixes the problem.

Like:

// Chane this:
List("ab").toSet.head.head

// Into this:
val set = List("ab").toSet
set.head.head
// This works because ...

// Another option is:
List("ab").toSet[String].head.head
// This also works because ...

@SethTisue
Copy link
Member

SethTisue commented Nov 28, 2023

Additionally, I think providing a concrete example of the suggested solutions would be useful, as well as explaining why that fixes the problem

I'm consciously choosing not to do this. In general, I seek to keep FAQ entries short and sweet. A brief answer, and then a link or two or three to whatever other sources on the Internet (or on our own documentation site) exist that expound further.

@SethTisue
Copy link
Member

That would mean, even if somebody accidentally grants an accepting review, don't merge it, as we're still mulling.

That was my intended meaning for marking it "draft", yeah.

@BalmungSan
Copy link
Contributor

I'm consciously choosing not to do this. In general, I seek to keep FAQ entries short and sweet. A brief answer, and then a link or two or three to whatever other sources on the Internet

Oh right, forgot about that.
In that case I still expect whatever link is chosen to have such examples and explanations :)
Be it the official Scala documentation or a StackOverflow question / answer as other entries.

@som-snytt
Copy link
Contributor Author

OK I will update with something like Seth's text. I'm having trouble with any title that says "dot". We say "dotted name" or similar for com.acme.foo, but "what do you call it when an expression uses a selection?" "Selection" seems too "specialized" a word for a FAQ. One reason this is a FAQ is that it's not obvious why I can't rewrite expressions in obvious ways and have it just work. You can say Liskov till you're blue in the face, referential transparency blah-blah, but if simple syntax tweaks break my code, then I will be confused and angry. I don't think a link to a ticket suffices, but a link to the Big Book of Scala can be supplied later.

@som-snytt
Copy link
Contributor Author

som-snytt commented Nov 28, 2023

ok I updated the section title.

toSet or not toSet, that is the question

I'm like, did I spell language right? It could be either affect or effect here, both work.


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).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall LGTM but I ask for one change: s/paradigmatic/common/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I accept pitching it to the commons

@som-snytt
Copy link
Contributor Author

som-snytt commented Nov 28, 2023

I neglected to mention that I inlined the links, to make it ASAP (short as preferred).

I changed paradigmatic question to stupid before the final edit.

I also changed toSet to toSeth.

@SethTisue SethTisue marked this pull request as ready for review November 28, 2023 18:16
@som-snytt
Copy link
Contributor Author

It should say, "If you are here, you should ask BalmungSan".

@SethTisue SethTisue merged commit 3ea84c7 into scala:main Nov 28, 2023
1 check passed
@som-snytt som-snytt deleted the issue/7743-toSet branch November 28, 2023 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants