18 May 2007 04:54
Re: Re: Decimal package
Erik Engbrecht <erik.engbrecht <at> gmail.com>
2007-05-18 02:54:46 GMT
2007-05-18 02:54:46 GMT
Yes, I've removed the references to the Boxed* classes and am using match as you described. However, I did run into some curious behavior.
So I had:
override def equals(that: Any): boolean = that match {
case that: Decimal[C] => equals(that)
case that: BigInt => equals(context(that))
case that: java.math.BigInteger=> equals(context(new BigInt(that)))
case that: Int => equals(context(that))
case that: Integer => equals(context(that.intValue))
case that: Long => equals(context(that.longValue))
case that: Number => {
try {
equals(context(that.doubleValue)) // construction may throw an exception depending on context
} catch {
case ar: ArithmeticException => false //conversion to a Decimal of this context failed
}
}
case _ => false
}
case that: Decimal[C] => equals(that)
case that: BigInt => equals(context(that))
case that: java.math.BigInteger=> equals(context(new BigInt(that)))
case that: Int => equals(context(that))
case that: Integer => equals(context(that.intValue))
case that: Long => equals(context(that.longValue))
case that: Number => {
try {
equals(context(that.doubleValue)) // construction may throw an exception depending on context
} catch {
case ar: ArithmeticException => false //conversion to a Decimal of this context failed
}
}
case _ => false
}
...and whenever it executed Number case, it returned false. I fixed the problem by adding a var to store the result calculated in the try/catch, but this doesn't seem like the right thing to do. Is there any way around it?
override def equals(that: Any): boolean = that match {
case that: Decimal[C] => equals(that)
case that: BigInt => equals(context(that))
case that: java.math.BigInteger=> equals(context(new BigInt(that)))
case that: Int => equals(context(that))
case that: Integer => equals(context(that.intValue))
case that: Long => equals(context( that.longValue))
case that: Number => {
var r: Boolean = false
try {
r = equals(context(that.doubleValue)) // construction may throw an exception depending on context
} catch {
case ar: ArithmeticException => r = false //conversion to a Decimal of this context failed
}
r
}
case _ => false
}
case that: Decimal[C] => equals(that)
case that: BigInt => equals(context(that))
case that: java.math.BigInteger=> equals(context(new BigInt(that)))
case that: Int => equals(context(that))
case that: Integer => equals(context(that.intValue))
case that: Long => equals(context( that.longValue))
case that: Number => {
var r: Boolean = false
try {
r = equals(context(that.doubleValue)) // construction may throw an exception depending on context
} catch {
case ar: ArithmeticException => r = false //conversion to a Decimal of this context failed
}
r
}
case _ => false
}
Anyway, fully commented (although possible not the brightest comments) code is attached. And now on to unit tests...
-Erik
On 16 May 2007 10:32:40 +0200, Lex Spoon <lex <at> lexspoon.org> wrote:
Gilles Dubochet <gilles.dubochet <at> epfl.ch> writes:
> However, in the soon-to-be-released Scala 2.5.0, boxing of native
> values will be changed to use the standard Java mechanism
> (java.lang.Number, etc. or the MSIL correspondent). This means that
> runtime.BoxedWhatever classes will be removed. I believe your code
> depends on the boxing being done that way.
In fact, in Scala code, you should be able to use just "Int" and
"Long", etc. The compiler will arrange that the numbers get unboxed
as needed.
So instead of:
case that: runtime.BoxedInt => equals(context(that.intValue))
case that: runtime.BoxedLong => equals(context(that.longValue))
You can do:
case that: Int => equals(context(that))
case that: Long => equals(context(that))
(Or if you can't, it looks like a bug to me.)
Lex
RSS Feed