Skip to content

rewrote singleton objects tour #704

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

Closed
wants to merge 1 commit into from

Conversation

travissarles
Copy link
Contributor

No description provided.

Methods and values that aren't associated with individual instances of a [class](classes.html) belong in *singleton objects*, denoted by using the keyword `object` instead of `class`.
Singleton objects are like classes in that they contain methods and values, but there are a few key differences.
* There is only one instance of a singleton object so their methods and values aren't associated with individual instances of a [class](classes.html).
* Objects have no constructors so they cannot be instantiated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This language has to be wrong and misleading. The spec says it nicely: "a single object of a new class." I might have preferred "a single instance of a new class." Occasionally, you have to be aware of when object init happens, including common use cases with circular definitions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@som-snytt Looking back on it, I agree that it's a bit misleading. I like "a single instance of a new class" except that the word "class" could be confusing for people who assume that classes can be instantiated. I'll try to explain that. Thanks for the feedback.

@travissarles travissarles force-pushed the singleton-objects branch 2 times, most recently from 398ed84 to 641d066 Compare February 20, 2017 12:57

# Defining a singleton object
Like with classes, the simplest form of an object is the keyword `object` and an identifier:
```tut
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why tut?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should be marking code blocks tut whenever possible, so PR validation checks that the code compiles. see #677


def blah = foo
}
The `class Event` imports `_isOnTheWeekend` from the `object Event`. To make a member inaccessible from outside, use `private[this]`.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it inaccessible from its companion class you mean. It is inaccessible everywhere else with just private

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I'll change the wording.


Note: If a class or object has a companion, both must be defined in the same file. To define them in the REPL, you must enter `:paste` and then paste in the class and companion object code.

## Sharing global state
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is such a bad practice I'm not sure it's a good idea to give it as an example. If yes, I'd change the tone and make it "Beware of global state", and end with a thread-safe example.


# Defining a singleton object
Like with classes, the simplest form of an object is the keyword `object` and an identifier:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t really understand the connection with classes, so I wouldn’ start with “like with classes”.


def blah = foo
object Event {
private def _isOnTheWeekend(date: LocalDateTime) = Array(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY).contains(date.getDayOfWeek)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure to see any advantage of putting this as a private def in the companion object. I think that maybe more compelling use cases for companion object’s methods are constants definition, default instances and alternative constructors.

@travissarles
Copy link
Contributor Author

@heathermiller ready to merge

@SethTisue
Copy link
Member

Both the old and new texts assume that object appears at the top level, in which case it is truly a singleton classloader-wide. But you can also nest object within other constructs and then there's more than one — it's only a singleton in that context.

Since the old text also had this problem, I think this PR could be merged anyway, but Travis, perhaps you're motivated to add some wording that addresses this? If not, perhaps we (or someone) can come back to it later.

Otherwise LGTM for merge.

@travissarles travissarles mentioned this pull request Mar 17, 2017
33 tasks
@som-snytt
Copy link
Contributor

Rebased at #978

@som-snytt som-snytt closed this Jan 10, 2018
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.

5 participants