Skip to content

Add self-type for Traits #86

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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/main/scala/stdlib/Traits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}