Skip to content

Instantly share code, notes, and snippets.

@shizone
Created December 6, 2011 14:22
Show Gist options
  • Save shizone/1438364 to your computer and use it in GitHub Desktop.
Save shizone/1438364 to your computer and use it in GitHub Desktop.
http://codr.cc/356776/rb を見て何となく
scala> sealed trait Color
defined trait Color
scala> case object Black extends Color
defined module Black
scala> case object Pink extends Color
defined module Pink
scala> case class Pants(color: Color)
defined class Pants
scala> trait HasLegs
defined trait HasLegs
scala> class Oneechan(val pants: Pants) extends HasLegs
defined class Oneechan
scala> val homuhomu = new Oneechan(Pants(Pink))
homuhomu: Oneechan = Oneechan@1baa8d8
scala> homuhomu.pants
res0: Pants = Pants(Pink)
scala> homuhomu.pants = Pants(Black)
<console>:15: error: reassignment to val
homuhomu.pants = Pants(Black)
^
scala> trait Tights { this: HasLegs =>
| def tightsColor: Color
| }
defined trait Tights
scala> val homuhomu =
| new Oneechan(Pants(Black)) with Tights {
| def tightsColor = Black
| }
homuhomu: Oneechan with Tights{def tightsColor: Black.type} = $anon$1@12a09d5
scala> homuhomu.tightsColor
res1: Black.type = Black
scala> class Incubator
defined class Incubator
scala> val qb =
| new Incubator with Tights {
| def tightsColor = Black
| }
<console>:13: error: illegal inheritance;
self-type Incubator with Tights does not conform to Tights's selftype Tights with HasLegs
new Incubator with Tights {
^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment