Skip to content

Rewrote self-type tour #748

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
May 19, 2017

Conversation

travissarles
Copy link
Contributor

No description provided.

@travissarles travissarles mentioned this pull request Mar 29, 2017
33 tasks
@travissarles travissarles requested a review from SethTisue May 12, 2017 11:41
@SethTisue
Copy link
Member

SethTisue commented May 15, 2017

hey I know I've done a lot of reviewing of these so it's logical to request me as a reviewer, but actually, can we spread it around more? I don't want to be in the position where it's always me. you can ask other members of the Scala team at Lightbend, Scala Center folks, etc.

previous-page: compound-types
prerequisite-knowledge: nested-classes, mixin-class-composition
---
When writing extensible code, it can be useful to declare a self-type as a means of dependency injection.
Copy link
Contributor

Choose a reason for hiding this comment

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

Not to minimize the difficulty, but this is quite opaque. Maybe something like: Self-types are a way to declare that a trait must be mixed into another trait, even though it doesn't directly extend it. That makes the members of the dependency available without imports.

---
When writing extensible code, it can be useful to declare a self-type as a means of dependency injection.

To use a self-type in a trait, write an identifier, the type of another trait to mix in, and a right fat arrow (e.g. `someIdentifier: SomeOtherTrait =>`).
Copy link
Contributor

Choose a reason for hiding this comment

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

The syntax is actually weird. Maybe be up-front about it. A self-type is a way to narrow the type of thisor another identifier that aliasesthis. The syntax looks like normal function syntax but means something entirely different. Darn, just noticed I introduced extra backticks.


Because we said `this: User =>` in `trait Tweeter`, now the variable `username` is in scope for the `tweet` method. This also means that since `VerifiedTweeter` extends `Tweeter`, it must also mix-in `User` (using `with User`).

Whenever a trait needs to mix-in another trait, a self-type can be used to bring the other trait's members into scope.
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, this could be said up front.

@som-snytt
Copy link
Contributor

@SethTisue you could put me on the reviewer list as "stuff Seth is sick of reviewing."

@travissarles
Copy link
Contributor Author

@SethTisue sure, no problem. I'll try to ping the Gitter channel again.

@travissarles travissarles force-pushed the explicit-self-reference branch from 8050e06 to 7fa2b19 Compare May 19, 2017 09:42
@travissarles travissarles force-pushed the explicit-self-reference branch from 7fa2b19 to ec6a1f2 Compare May 19, 2017 11:48
@travissarles travissarles merged commit 2c1bf67 into scala:master May 19, 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.

3 participants