Skip to content

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

Merged
merged 1 commit into from
Apr 4, 2017
Merged

new classes tour #698

merged 1 commit into from
Apr 4, 2017

Conversation

travissarles
Copy link
Contributor

No description provided.

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.

Copy link
Contributor

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")
Copy link
Contributor

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`
Copy link
Contributor

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"
}
```
Copy link
Contributor

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:
Copy link
Contributor

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.
Copy link
Contributor

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.

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_.

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.

Copy link
Contributor Author

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:

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.

Copy link
Contributor Author

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:

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.

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.

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?

Copy link
Contributor

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

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.

```
class Point(val x: Int, val y: Int)
val point = new Point(1, 2)
val.x = 3 // <-- does not compile
Copy link
Member

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
@travissarles
Copy link
Contributor Author

@heathermiller ready to merge

Copy link
Member

@SethTisue SethTisue left a 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.

@travissarles travissarles mentioned this pull request Mar 17, 2017
33 tasks
@SethTisue SethTisue merged commit 978920c into scala:master Apr 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants