The Artima Developer Community
Sponsored Link

Akka Concurrency Forum
On traits vs. Params

2 replies on 1 page. Most recent reply: Dec 6, 2012 12:43 AM by Derek Wyatt

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 2 replies on 1 page
Derek Wyatt

Posts: 69
Nickname: dwyatt
Registered: Oct, 2012

On traits vs. Params Posted: Dec 5, 2012 5:25 PM
Reply to this message Reply
Advertisement
Ryan LeCompte noted that I tend to use traits a fair bit. There's a couple of reasons for this... well, there's really only one... traits are awesome. Traits are possibly the best part of Scala. I don't use them in the book to near their potential, mostly because I don't need to and partly because using them too much would make the book less accessible.

For example, in the Beacon, I have a parameter that lets the guy doing the construction specify the heading that the Beacon should send out. The beaconInterval, on the other hand, is a property of a Trait. Why the distinction?

Partly because I can and partly because I think it's "right".

The beaconInterval isn't the concern of the guy doing the construction; he just wants the thing to 'ping' a certain heading and he couldn't care less how often it does it so long as it adheres to the manufacturer's specification. What he does want to do is set the heading, which he can do at construction time.

Now, for the purposes of testing the guy who built the Beacon wants to modify the interval so he can speed up his test, and Traits provide an awesome way to do that. We don't need to derive a new Beacon, just mix in a new Trait.

We get the greatness of modification without having to do a silly inheritance, and also without having to expose the silliness to the user of the class. Fancy.

Now, this doesn't mean this is the only way to do it, and it might not be the way you'd do it, but it's the way I did it :)


Ryan LeCompte

Posts: 1
Nickname: 84831
Registered: Nov, 2012

Re: On traits vs. Params Posted: Dec 5, 2012 5:39 PM
Reply to this message Reply
Hi Derek,

Thanks a ton for elaborating further on your reasoning! I think this approach of breaking out configuration parameters of a class like this strikes me as a little odd coming from the Ruby world. Specifically, in this example I would just use a default parameter for the Beacon, something like:

class Beacon(heading: Float, beaconInterval: Duration = 1.second) extends Actor {
...
}

This way, the person that's constructing the instance doesn't really care about setting the beaconInterval to something more specific if he doesn't care about it. If you want to specify a different value for testing, then you would just specify it when you construct the class.

Now, I do like using traits for dependency injection when the cake pattern is used. In that case, I feel like the cake pattern is useful for specifying dependencies on other services/objects, not on individual fields of the class itself. I feel like breaking out the specification of individual data fields of a class into separate traits is sort of teasing apart the core internal implementation unnecessarily. I guess I could be wrong here.

Note that this was just a minor thing that I noticed in the examples in the book. In the majority of the examples, I noticed that you used traits in a cake-pattern-like-way to specify dependencies on other actors. I thought it was a great way to do that (i.e, a "Provider" trait, etc).

The book is awesome! I am currently on the remote actors chapter. :)

Thanks!

Ryan

Derek Wyatt

Posts: 69
Nickname: dwyatt
Registered: Oct, 2012

Re: On traits vs. Params Posted: Dec 6, 2012 12:43 AM
Reply to this message Reply
To each his own. :)

w.r.t to traits and the cake pattern, you might be interested in Daniel Spiewak's article on existential types: https://www.precog.com/blog-precog-2/entry/existential-types-ftw.

Flat View: This topic has 2 replies on 1 page
Topic: actorFor Outside ActorSystem Previous Topic   Next Topic Topic: Import for Duration syntactic sugar

Sponsored Links



Google
  Web Artima.com   

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