Article Discussion
Scala's Selfless Trait Pattern
Summary: This article describes a simple Scala design pattern that allows library designers to provide services that their clients can access either through mixins or imports. Giving users a choice between mixin composition and importing makes a library easier to use.
5 posts on 1 page.      
« Previous 1 Next »
The ability to add new comments in this discussion is temporarily disabled.
Most recent reply: September 30, 2009 4:29 AM by Antonio R.
Bill
Posts: 408 / Nickname: bv / Registered: January 17, 2002 4:28 PM
Scala's Selfless Trait Pattern
September 4, 2009 9:00 AM      
This article describes a simple Scala design pattern that allows library designers to provide services that their clients can access either through mixins or imports. Giving users a choice between mixin composition and importing makes a library easier to use.

http://www.artima.com/scalazine/articles/selfless_trait_pattern.html

What do you think of the Selfless Trait pattern?
Daniel
Posts: 11 / Nickname: djimenez / Registered: December 22, 2004 0:48 AM
Re: Scala's Selfless Trait Pattern
September 9, 2009 6:48 AM      
Before JDK 5 and its static imports, I would often define abstract classes with protected constructors and protected-static-final methods to sort of support this. That way, people could subclass it to avoid qualified names, or import it if the single inheritance chain was already taken.

Of course, the tools Scala provides are much more sophisticated - in the good way - and make this a lot cleaner.

Do you remember what first tipped you off that this could be done?
Bill
Posts: 408 / Nickname: bv / Registered: January 17, 2002 4:28 PM
Re: Scala's Selfless Trait Pattern
September 9, 2009 9:07 AM      
> Do you remember what first tipped you off that this could
> be done?
>
I think it was born out of need. I wanted at one point to exercise ScalaTest's ShouldMatchers DSL in the interpreter, and realized it was not easy. It hit me that if I made a companion object to ShouldMatchers that mixed in the ShouldMatchers trait, it would let me import the whole DSL and use it in the interpreter. I then went looking for other opportunities to make the same thing possible. I did this with Assertions, but there may still be some more traits in ScalaTest I can do this with, because I think I may have one or two traits in there with self types that aren't necessary and should be removed. One of the items on my very short list of remaining things to do for the 1.0 release is look for extraneous self types that I can remove, and then apply this pattern to the trait.
Raoul
Posts: 20 / Nickname: raoulduke / Registered: April 14, 2006 11:48 AM
Re: Scala's Selfless Trait Pattern
September 17, 2009 2:51 PM      
> This article describes a simple Scala design pattern

based on what i've see on the scala mailing lists, these things turn out to easily not be that simple in real world situations. so i'm always a little wary of claims of it slices it dices now how much would you pay. :-)
Antonio R.
Posts: 1 / Nickname: rodant / Registered: January 26, 2005 10:39 PM
Re: Scala's Selfless Trait Pattern
September 30, 2009 4:29 AM      
I would remark, that in order the import-variant to be an equivalent alternative, the library trait must be stateless and full functional. I found the article helpful, thanks for stating the pattern clearly.

Cheers.
5 posts on 1 page.
« Previous 1 Next »