• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Scala Self Traits

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would the Wrong trait result in a compile time failure in the following code?



Does it mean that I always have to extend Tweeter with a User?
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm slowly trying to get a hang of it. In essence this is Scala's native way of doing DI! But what I don't get it:

Tweeter has-a User, then why would one need to mix User in when extending Tweeter? Scala's language features just seem like an abyss!
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rule is anybody that extends Tweeter (trait, abstract class, object, class) should either declare User as a self type as declared in Tweeter or bloody mix it in!
 
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Self type annotation is used for dependency injection for traits which basically what Cake Pattern is.
Consider it as Tweeter needs an implementation of User instead of considering it as Tweeter has-a User. So, whenever you would like to use Tweeter, you'd have to provide an implementation of User.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The self-type annotation in your trait Tweeter does not mean that Tweeter has-a User, it says that Tweeter is-a user, or rather that anything that extends Tweeter must also be a User.

It puts a restriction on traits and classes that extend Tweeter; the restriction is that those must also extend User.

You can use this for mix-ins. So, Tweeter is a trait that adds extra functionality for things that already extend User.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic