Skip to content

Instantly share code, notes, and snippets.

@noahlz
Created September 1, 2013 03:13
Show Gist options
  • Save noahlz/6402107 to your computer and use it in GitHub Desktop.
Save noahlz/6402107 to your computer and use it in GitHub Desktop.
Understanding Scala's self-typed traits
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