-
Notifications
You must be signed in to change notification settings - Fork 1k
small changes to basics #751
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
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.
LGTM — I have made some suggestions, but I don't insist on any of them.
@@ -136,7 +141,7 @@ def add(x: Int, y: Int): Int = x + y | |||
println(add(1, 2)) // 3 | |||
``` | |||
|
|||
Methods cannot be named with the `val` or `var` keywords. | |||
Notice how the return type is declared _after_ the parameter list: `: Int`. Methods cannot be named with the `val` or `var` keywords. |
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.
Methods cannot be named with the
val
orvar
keywords.
not sure about this addition. is this really needed here? if so, I think you need to expand on this and be clearer. (else, omit?) if you want to include it, I would suggest stating it positively rather than negatively: "A method is always defined using the def
keyword."
@@ -160,6 +165,16 @@ println("Hello, " + name + "!") | |||
|
|||
There are some other differences, but for now, you can think of them as something similar to functions. | |||
|
|||
Method can have multi-line expressions as well. |
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.
"Methods" or "A method". Maybe "A method's body"?
square.toString | ||
} | ||
``` | ||
You can use the `return` but it is not necessary or idiomatic. Expressions |
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 would downplay and discourage return
more strongly. Perhaps: "The last expression in the body is the method's return value. (Scala does have a return
keyword, but it's rarely used.)"
@@ -170,6 +185,7 @@ class Greeter(prefix: String, suffix: String) { | |||
println(prefix + name + suffix) | |||
} | |||
``` | |||
The return type of the method `greet` is `Unit` which says there's nothing meaningful to return. It's used similarly to `void` in Java and C but the main difference is that there is actually a singleton value of type `Unit`, written `()`. |
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.
perhaps:
The return type of the method greet
is Unit
, which says there's nothing meaningful to return. It's used similarly to void
in Java and C. (A difference is that because every Scala expression must have some value, there is actually a singleton value of type Unit
, written ()
. It carries no information.)
No description provided.