The Artima Developer Community
Sponsored Link

Articles Forum
Scala's Selfless Trait Pattern

4 replies on 1 page. Most recent reply: Sep 30, 2009 5:29 AM by Antonio R. Rodríguez S.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Scala's Selfless Trait Pattern Posted: Sep 4, 2009 10:00 AM
Reply to this message Reply
Advertisement
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 Jimenez

Posts: 40
Nickname: djimenez
Registered: Dec, 2004

Re: Scala's Selfless Trait Pattern Posted: Sep 9, 2009 7:48 AM
Reply to this message Reply
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 Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Scala's Selfless Trait Pattern Posted: Sep 9, 2009 10:07 AM
Reply to this message Reply
> 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 Duke

Posts: 127
Nickname: raoulduke
Registered: Apr, 2006

Re: Scala's Selfless Trait Pattern Posted: Sep 17, 2009 3:51 PM
Reply to this message Reply
> 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. Rodríguez S.

Posts: 6
Nickname: rodant
Registered: Jan, 2005

Re: Scala's Selfless Trait Pattern Posted: Sep 30, 2009 5:29 AM
Reply to this message Reply
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.

Flat View: This topic has 4 replies on 1 page
Topic: Time is the New Memory Previous Topic   Next Topic Topic: Skinning and Components in Flex 4's Spark Architecture

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use