From 3248d91f66d825de73e636ed175534eff54cb99b Mon Sep 17 00:00:00 2001 From: Joe Conley Date: Fri, 28 Apr 2017 10:43:19 -0400 Subject: [PATCH] Add self-type for Traits --- src/main/scala/stdlib/Traits.scala | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/scala/stdlib/Traits.scala b/src/main/scala/stdlib/Traits.scala index 9898dc38..a2530e86 100644 --- a/src/main/scala/stdlib/Traits.scala +++ b/src/main/scala/stdlib/Traits.scala @@ -100,5 +100,24 @@ object Traits extends FlatSpec with Matchers with org.scalaexercises.definitions myListener.isInstanceOf[Any] should be(res2) myListener.isInstanceOf[AnyRef] should be(res3) } - + + /** Traits also can use self-types. A self-type lists the required dependencies for mixing in the trait. When mixing in the main trait, all self-type dependencies of that trait must also be mixed in, otherwise a compile-time error is thrown. + * + * Also, the dependencies can't have identical method/property names or else you'll get an `illegal inheritance` error. + */ + def selfTypeTraits(res0: Int){ + trait B { + def bId = 2 + } + + trait A { + self: B => + + def aId = 1 + } + + //val a = new A //***does not compile!!!*** + val obj = new A with B + (obj.aId + obj.bId) should be(res0) + } }