-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Rewrote self-type tour #748
Conversation
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. |
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.
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 =>`). |
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.
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 aliases
this. 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. |
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.
Yes, this could be said up front.
@SethTisue you could put me on the reviewer list as "stuff Seth is sick of reviewing." |
@SethTisue sure, no problem. I'll try to ping the Gitter channel again. |
8050e06
to
7fa2b19
Compare
7fa2b19
to
ec6a1f2
Compare
No description provided.