-
Notifications
You must be signed in to change notification settings - Fork 1k
Resolving TODO tags, Part 2 #1874
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
Conversation
> If you’re interested in how methods can be used like functions, see the [Eta Expansion][eta] discussion. | ||
|
||
|
||
|
||
## Examples of impure functions | ||
|
||
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 typeUnit
.
Because it doesn’t return anything, logically the only reason you ever call it is to achieve some side effect.
Similarly, any method that returnsUnit
is a candidate to be an impure function.
There was a problem hiding this comment.
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"?
|
||
The `foreach` method on collections classes is impure because it’s only used for its side effects, such as printing to STDOUT. | ||
|
||
> 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. |
There was a problem hiding this comment.
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
.
There was a problem hiding this 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.
Co-authored-by: Jonathan <[email protected]>
Co-authored-by: Jonathan <[email protected]>
Co-authored-by: Jonathan <[email protected]>
@@ -398,7 +397,12 @@ For more details, see the remainder of these modeling lessons. | |||
## Abstract classes | |||
|
|||
{% 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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: |
This is a second batch of trying to resolve my TODO tags in the documents.