Scala: Difference between revisions
Jump to navigation
Jump to search
Hoppinglife (talk | contribs) |
Hoppinglife (talk | contribs) |
||
| Line 8: | Line 8: | ||
A trait with a self type cannot be used without mixed in that trait first. | A trait with a self type cannot be used without mixed in that trait first. | ||
< | |||
<syntaxhighlight lang="scala"> | |||
trait User { | trait User { | ||
def username: String | def username: String | ||
| Line 17: | Line 18: | ||
def tweet(tweetText: String) = println(s"$username: $tweetText") | def tweet(tweetText: String) = println(s"$username: $tweetText") | ||
} | } | ||
</ | </syntaxhighlight> | ||
A question is how it is different from trait subclasses. | A question is how it is different from trait subclasses. | ||
See [https://docs.scala-lang.org/tour/self-types.html Official tour], [http://jonasboner.com/real-world-scala-dependency-injection-di/ a discussion on the cake pattern] and [https://stackoverflow.com/questions/1990948/what-is-the-difference-between-self-types-and-trait-subclasses a StackOverflow question] about this. | See [https://docs.scala-lang.org/tour/self-types.html Official tour], [http://jonasboner.com/real-world-scala-dependency-injection-di/ a discussion on the cake pattern] and [https://stackoverflow.com/questions/1990948/what-is-the-difference-between-self-types-and-trait-subclasses a StackOverflow question] about this. | ||