The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Error in Chapter 19, Section 4?

1 reply on 1 page. Most recent reply: Jan 5, 2008 7:10 AM by Dianne Marsh

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 1 reply on 1 page
Dianne Marsh

Posts: 4
Nickname: dmarsh
Registered: Oct, 2007

Error in Chapter 19, Section 4? Posted: Jan 4, 2008 8:24 AM
Reply to this message Reply
Advertisement
19.4 Converting the Receiver, page 356

I'm using the example in the book:


class Rational(n: Int, d: Int) {
val numer: Int = n
val denom: Int = d

def + (that: Rational): Rational =
new Rational(numer * that.denom + that.numer*denom,
denom * that.denom)
def - (that: Rational): Rational =
new Rational(numer * that.denom - that.numer * denom,
denom * that.denom)

def + (that: Int): Rational =
new Rational(numer * 1 + that * denom,
denom)

implicit def intToRational(x: Int) = new Rational(x, 1)



override def toString() = numer+"/"+denom
}

// test code
val x = new Rational(1, 2)
println(x)

val y = x + 1
println(y)

val z = 1 + x



But instead of getting the implicit conversion, I get a compiler error on the line above (saying that the
overloaded method value + cannot be applied to (Rational).

Have I mistyped something? Do I have an error or is this a compiler error?

-- Dianne


Dianne Marsh

Posts: 4
Nickname: dmarsh
Registered: Oct, 2007

Re: Error in Chapter 19, Section 4? Posted: Jan 5, 2008 7:10 AM
Reply to this message Reply
Never mind! A mistype!

Of course, the implicit conversion belongs OUTSIDE of the class definition.

Dianne

Flat View: This topic has 1 reply on 1 page
Topic: 08IEEE intel wireless commnication conference Previous Topic   Next Topic Topic: Licence of example code in book

Sponsored Links



Google
  Web Artima.com   

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