Created
September 1, 2013 03:13
-
-
Save noahlz/6402107 to your computer and use it in GitHub Desktop.
Understanding Scala's self-typed traits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Huggable { | |
self: Pet => | |
def hug() :Unit = println("Dawww...!") | |
} | |
class Pet | |
class Puppy extends Pet with Huggable | |
class Kitten extends Huggable | |
// Compilation error! | |
// [error] /home/noahlz/projects/prog-scala/src/main/scala/app/SelfTyped.scala:12: illegal inheritance; | |
// [error] self-type app.Kitten does not conform to app.Huggable's selftype app.Huggable with app.Pet | |
// [error] class Kitten extends Huggable | |
// [error] ^ | |
// [error] one error found | |
object SelfTypedMain extends App { | |
new Puppy().hug | |
new Kitten().hug | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment