-
Notifications
You must be signed in to change notification settings - Fork 1k
Added an example on how to use anonymous functions in the "Anonymous … #628
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
Changes from 1 commit
4f0066e
db85835
4b2a761
720db74
78180d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,3 +51,33 @@ Function1[Int, Int] | |
Function2[Int, Int, String] | ||
Function0[String] | ||
``` | ||
|
||
The following example shows how to use anonymous function of the beginning of this page | ||
|
||
```tut | ||
package tour | ||
|
||
object AnonymousFunction { | ||
|
||
/** | ||
* Method to increment an integer by one. | ||
*/ | ||
var anonymousIncrementFunction = (x: Int) => x + 1 | ||
|
||
/** | ||
* Main method | ||
* @param args application arguments | ||
*/ | ||
def main(args: Array[String]) { | ||
|
||
// Create an integer to test the anonymous function with | ||
var myInteger: Int = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest omitting You can omit the semicolon too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated that part. |
||
|
||
println(myInteger) // Prints: 0 | ||
|
||
myInteger = anonymousIncrementFunction(myInteger) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest that the example avoid using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't ommit the var in the two places it appeared, else the code did not compile. I guess it is 'truly needed' in those cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could do something like this to show that a previously incremented val will be incremented again:
It is more idiomatic to assign a result to a new val rather than reusing it. |
||
|
||
println(myInteger) // Prints: 1 | ||
} | ||
} | ||
``` |
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.
The
tut
error you're getting will go away if you simply omit this line, which isn't necessary anyway.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.
I removed the 'package tour'