Articles
|
News
|
Weblogs
|
Books
|
Forums
Artima Forums
|
Articles
|
Weblogs
|
Java Answers
|
News
Sponsored Link
•
Programming in Scala Forum
get rid of Rational's 'private val g'?
7
replies on
1
page.
Most recent reply
: Dec 23, 2010 10:55 AM by
Jim Balter
Welcome
Guest
Sign In
Back to Topic List
Reply to this Topic
Search Forum
Threaded View
Previous Topic
Next Topic
Flat View: This topic has
7
replies on
1
page
Rob Dickens
Posts: 15
Nickname: robcd
Registered: Feb, 2008
get rid of Rational's 'private val g'?
Posted: Dec 18, 2010 8:27 AM
Reply
Advertisement
Dear fellow readers,
I refer to Listing 6.3 on p148 (p138 of 1st edition).
g is only required in order to calculate numer and denom, and therefore doesn't deserve its broader scope, and to be kept around.
In Java, g could be got rid of using blank finals (for numer and denom).
The next best thing in Scala would be to put numer and denom in a tuple, which would get rid of g, but be less satisfactory otherwise.
Anyone have a better solution?
Thanks,
Rob
Jim Balter
Posts: 13
Nickname: jibal
Registered: Dec, 2010
Re: get rid of Rational's 'private val g'?
Posted: Dec 21, 2010 10:14 PM
Reply
I would expect the compiler to eliminate g.
Rob Dickens
Posts: 15
Nickname: robcd
Registered: Feb, 2008
Re: get rid of Rational's 'private val g'?
Posted: Dec 21, 2010 11:30 PM
Reply
Hi. In which case g wouldn't be 'kept around', taking up space. That would indeed solve half the problem.
However, from the programmer's point of view, the unwarranted scope is still off-putting.
Jim Balter
Posts: 13
Nickname: jibal
Registered: Dec, 2010
Re: get rid of Rational's 'private val g'?
Posted: Dec 22, 2010 12:51 AM
Reply
Try
val (numer, denom) = { val g = gcd(n.abs, d.abs); (n/g, d/g) }
Rob Dickens
Posts: 15
Nickname: robcd
Registered: Feb, 2008
Re: get rid of Rational's 'private val g'?
Posted: Dec 22, 2010 2:33 AM
Reply
Right - this is the one solution I mention in my post - use a tuple.
However, I didn't really explain why I thought it was unsatisfactory.
On second thoughts, 'val (numer, denom)' does after all define a 'val numer' and a 'val denom'.
But is it truly equivalent to defining two separate vals, when viewed from the outside?
Jim Balter
Posts: 13
Nickname: jibal
Registered: Dec, 2010
Re: get rid of Rational's 'private val g'?
Posted: Dec 22, 2010 12:22 PM
Reply
Yes, it is equivalent. val (numer, denom) is a pattern match, not a tuple. It is matched against the tuple (n/g, d/g) to decompose it. (If for some reason you object to using tuples as intermediate values, don't use Scala.)
Jim Balter
Posts: 13
Nickname: jibal
Registered: Dec, 2010
Re: get rid of Rational's 'private val g'?
Posted: Dec 23, 2010 10:35 AM
Reply
However, as noted on the Scala mailing list where you inquired, and at
http://stackoverflow.com/questions/1118669/how-do-you-define-a-local-var-val-in-the-primary-constructor-in-scala/1118984,
that secretly adds a useless field to the class.
Jim Balter
Posts: 13
Nickname: jibal
Registered: Dec, 2010
Re: get rid of Rational's 'private val g'?
Posted: Dec 23, 2010 10:55 AM
Reply
See
http://stackoverflow.com/questions/1218872/avoiding-scala-memory-leaks-scala-constructors/1451024#1451024
for a solution that avoids extra fields (by having a private primary constructor that takes g as a parameter).
Flat View: This topic has
7
replies on
1
page
Previous Topic
Next Topic
Sponsored Links
Web
Artima.com
Copyright
© 1996-2019 Artima, Inc. All Rights Reserved. -
Privacy Policy
-
Terms of Use