-
Notifications
You must be signed in to change notification settings - Fork 1k
new classes tour #698
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
new classes tour #698
Conversation
6333271
to
78db2e8
Compare
Classes in Scala are parameterized with constructor arguments. The code above defines two constructor arguments, `x` and `y`; they are both visible in the whole body of the class. | ||
This `Point` class has four members: the variables `x` and `y` and the methods `move` and | ||
`toString`. Unlike many other languages, the primary constructor is in the class signature `(var x: Int, var y: Int)`. The `move` method takes two integer arguments but does not return a value (the return type `Unit` corresponds to `void` in Java-like languages). `toString`, on the other hand, does not take any parameters but returns a `String` value. Since `toString` overrides the inherited `toString` method, it is tagged with the `override` keyword. | ||
|
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.
Before jumping to secondary constructors I would put an example of instantiation and member calls.
class Point { | ||
def this(x: Int, y: Int) { | ||
this() | ||
if (x > 100 || y > 100) println("Warning: Out of bounds") |
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.
This example is confusing since the x
and y
parameters are eventually discarded.
} | ||
} | ||
``` | ||
There are few requirements for auxiliary constructors: | ||
* They must start with `def this` | ||
* The parameters must _not_ be declared `val` or `var` |
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 say “can not”
override def toString: String = | ||
s"Point: ($x, $y), Distance from origin: $distanceFromOrigin" | ||
} | ||
``` |
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.
Minor remark: usually private members are used to hide state from the outside. In your example that’s the opposite situation: private members are pure, while public members x
and y
are vars.
``` | ||
|
||
You can also simply nest private methods inside of the public ones | ||
that use them: |
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 think this is a general point related to scopes more than classes. I’m not sure it’s best place is in this page.
|
||
Parameters without `val` or `var` are private values, visible only within the class and its subclasses. |
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.
Actually they are not visible from subclasses.
78db2e8
to
79fbccd
Compare
Classes in Scala are static templates that can be instantiated into many objects at runtime. | ||
Here is a class definition which defines a class `Point`: | ||
Classes in Scala are blueprints for creating objects. They can contain methods, | ||
values, and variables, which are collectively called _members_. |
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.
They can also contain types, objects, traits, and classes which are also members.
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 was going to add this later in a "nesting" tutorial but to be accurate, we need to mention it now and say "more on that later".
|
||
## Defining a class | ||
A minimal class definition is simply the keyword `class` and | ||
an identifier: |
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.
It's important to note that class names should be capitalized because it's a universal convention and it has a downstream effect on pattern-matching.
A minimal class definition is simply the keyword class and an identifier. Class names should be capitalized.
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 we should sprinkle naming conventions throughout the tour. It's easier than looking at a big naming-convention document, though both are important.
|
||
val user1 = new User | ||
``` | ||
The keyword `new` is used to create an instance of the class. `User` has an implicit constructor which takes no arguments because no constructor was defined. However, you'll often want a constructor and class body. Here is an example class definition for a point: |
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 say "default" rather than "implicit" here to avoid confusion with implicit
which is an unrelated concept.
``` | ||
|
||
Classes in Scala are parameterized with constructor arguments. The code above defines two constructor arguments, `x` and `y`; they are both visible in the whole body of the class. | ||
This `Point` class has four members: the variables `x` and `y` and the methods `move` and | ||
`toString`. Unlike many other languages, the primary constructor is in the class signature `(var x: Int, var y: Int)`. The `move` method takes two integer arguments but does not return a value (the return type `Unit` corresponds to `void` in Java-like languages). `toString`, on the other hand, does not take any parameters but returns a `String` value. Since `toString` overrides the inherited `toString` method, it is tagged with the `override` keyword. |
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.
For "but does not return a value (the return type Unit
corresponds to void
in Java-like languages)" I suggest
and returns the Unit value ()
, which carries no information. This corresponds roughly with void
in Java-like languages.
``` | ||
|
||
Classes in Scala are parameterized with constructor arguments. The code above defines two constructor arguments, `x` and `y`; they are both visible in the whole body of the class. | ||
This `Point` class has four members: the variables `x` and `y` and the methods `move` and | ||
`toString`. Unlike many other languages, the primary constructor is in the class signature `(var x: Int, var y: Int)`. The `move` method takes two integer arguments but does not return a value (the return type `Unit` corresponds to `void` in Java-like languages). `toString`, on the other hand, does not take any parameters but returns a `String` value. Since `toString` overrides the inherited `toString` method, it is tagged with the `override` keyword. |
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.
toString overrides the inherited toString method
Inherited from what? We haven't talked about subclassing. Do we need to do this here?
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 can maybe just add a link to the “unified types” page.
|
||
This is also a good practice to enhance clarity. | ||
|
||
## Member scope |
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 suggest renaming this section something like Private Members and Getter/Setter Syntax.
79fbccd
to
082bc52
Compare
``` | ||
class Point(val x: Int, val y: Int) | ||
val point = new Point(1, 2) | ||
val.x = 3 // <-- does not compile |
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 think you mean point.x
, not val.x
.
If it's not appropriate to introduce copy
here, maybe at least add a note that links forward to where copy
will be introduced?
added procedures as prereq
082bc52
to
3df470b
Compare
@heathermiller ready to merge |
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'm not very comfortable with having var
so front-and-center here... but, this is certainly a huge improvement overall regardless, so I would say let's merge it, and perhaps come back and handle var
differently in a future revision.
No description provided.