From 266b0e8e978408aa84f7c4831d17b59aea6172f5 Mon Sep 17 00:00:00 2001 From: Luc Henninger Date: Thu, 22 Sep 2022 19:42:58 +0200 Subject: [PATCH 1/2] Add code tabs for _tour/self-types --- _tour/self-types.md | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/_tour/self-types.md b/_tour/self-types.md index 49110f23f9..63561e5794 100644 --- a/_tour/self-types.md +++ b/_tour/self-types.md @@ -16,6 +16,9 @@ Self-types are a way to declare that a trait must be mixed into another trait, e A self-type is a way to narrow the type of `this` or another identifier that aliases `this`. The syntax looks like normal function syntax but means something entirely different. To use a self-type in a trait, write an identifier, the type of another trait to mix in, and a `=>` (e.g. `someIdentifier: SomeOtherTrait =>`). + +{% tabs self-types_1 class=tabs-scala-version %} +{% tab 'Scala 2' for=self-types_1 %} ```scala mdoc trait User { def username: String @@ -33,5 +36,25 @@ class VerifiedTweeter(val username_ : String) extends Tweeter with User { // We val realBeyoncé = new VerifiedTweeter("Beyoncé") realBeyoncé.tweet("Just spilled my glass of lemonade") // prints "real Beyoncé: Just spilled my glass of lemonade" ``` - 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`). + +{% endtab %} +{% tab 'Scala 3' for=self-types_1 %} +```scala +trait User: + def username: String + +trait Tweeter: + this: User => // reassign this + def tweet(tweetText: String) = println(s"$username: $tweetText") + +class VerifiedTweeter(val username_ : String) extends Tweeter, User: // We mixin User because Tweeter required it + def username = s"real $username_" + +val realBeyoncé = new VerifiedTweeter("Beyoncé") +realBeyoncé.tweet("Just spilled my glass of lemonade") // prints "real Beyoncé: Just spilled my glass of lemonade" +``` +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 `, User`). + +{% endtab %} +{% endtabs %} From d59a94d8b5440e1ad839b030e8562b5e6d9f3adc Mon Sep 17 00:00:00 2001 From: Luc Henninger Date: Fri, 23 Sep 2022 20:02:48 +0200 Subject: [PATCH 2/2] Update _tour/self-types.md Yes. Co-authored-by: Jamie Thompson --- _tour/self-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_tour/self-types.md b/_tour/self-types.md index 63561e5794..f209fd5101 100644 --- a/_tour/self-types.md +++ b/_tour/self-types.md @@ -51,7 +51,7 @@ trait Tweeter: class VerifiedTweeter(val username_ : String) extends Tweeter, User: // We mixin User because Tweeter required it def username = s"real $username_" -val realBeyoncé = new VerifiedTweeter("Beyoncé") +val realBeyoncé = VerifiedTweeter("Beyoncé") realBeyoncé.tweet("Just spilled my glass of lemonade") // prints "real Beyoncé: Just spilled my glass of lemonade" ``` 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 `, User`).