Skip to content

Conversation

@alvinj
Copy link
Contributor

@alvinj alvinj commented Dec 24, 2020

This is a second batch of trying to resolve my TODO tags in the documents.

@b-studios b-studios added the scala-3 scala 3 documentation label Jan 4, 2021
@b-studios b-studios self-requested a review January 5, 2021 12:57
@b-studios b-studios self-assigned this Jan 5, 2021
Conversely, the following functions are *impure* because they violate the definition.
Conversely, the following functions are _impure_ because they violate the definition.

The `foreach` method on collections classes is impure because it’s only used for its side effects, such as printing to STDOUT.
Copy link
Contributor

Choose a reason for hiding this comment

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

In fact, this is not true. It will probably be called with impure functions but List(1,2,3).foreach { () } is perfectly pure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there any way to reword this, or is it best to just delete the foreach sentence and the three bullet points after it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you want to keep this discussion, here’s a stab at rewording it. FWIW, the main reason I like this discussion is that it helps people keep an eye out for methods that return Unit. I know it was something I never considered many years ago. Here’s the rewording:

While the foreach method on collections classes is technically pure, it’s typically used for side effects, such as printing to STDOUT.

A great hint that foreach might be used for side effects is that it returns the type Unit.
Because it doesn’t return anything, logically the only reason you ever call it is to achieve some side effect.
Similarly, any method that returns Unit is a candidate to be an impure function.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for not following up earlier. I think your argument is perfectly sensible. Maybe it is easiest to actually say exactly that: "Keep an eye out for methods that return Unit. Since they do not return anything useful, this return type indicates that they are only called for their side effects"?


> A great hint that `foreach` is impure is that it’s method signature declares that it returns the type `Unit`.
> Because it doesn’t return anything, logically the only reason you ever call it is to achieve some side effect.
> Similarly, *any* method that returns `Unit` is going to be an impure function.
Copy link
Contributor

Choose a reason for hiding this comment

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

Like foreach above, not completely true. Unit only indicates that it might be executed for its side-effects. You can have (useless) pure methods that return Unit.

Copy link
Contributor

@b-studios b-studios left a comment

Choose a reason for hiding this comment

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

@alvinj maybe you can look again at the definition of a pure function. Otherwise this PR mostly affects formatting and comments and looks good to me.

@b-studios b-studios assigned alvinj and unassigned b-studios Jan 6, 2021
alvinj and others added 4 commits January 7, 2021 17:32
Co-authored-by: Jonathan <jonathan@b-studios.de>
Co-authored-by: Jonathan <jonathan@b-studios.de>
Co-authored-by: Jonathan <jonathan@b-studios.de>

{% comment %}
TODO: I have some notes on when to use abstract classes, and can update this section.
LATER: If anyone wants to update this section, our comments about abstract classes and traits are on Slack. The biggest points seem to be:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
LATER: If anyone wants to update this section, our comments about abstract classes and traits are on Slack. The biggest points seem to be:
LATER: If anyone wants to update this section, here are a few points that could be mentioned:

Base automatically changed from master to main March 23, 2021 13:44
@b-studios b-studios merged commit 8a66438 into scala:main May 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scala-3 scala 3 documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants