The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Question about implicit conversions

3 replies on 1 page. Most recent reply: May 18, 2009 10:18 PM by Sergei Drannikov

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 3 replies on 1 page
Paul Carter

Posts: 2
Nickname: 65998
Registered: Apr, 2009

Question about implicit conversions Posted: Apr 22, 2009 2:29 PM
Reply to this message Reply
Advertisement
I am obviously missing something about them. In Chapter 6, it's said that an implicit method can be used to convert objects automatically. However, in the two simple classes below, I don't see this happening:
File test/A.scala:

package test

object A {
implicit def convert( x: Int) = {
println("convert")
new A(x)
}
}

class A( i : Int ) {
val v = i
override def toString() = i.toString

def m () = {
this
}

def + ( r : A) = {
i + r.v
}
}


File: test/B.scala


package test

object B {

def m1() = {
val a : A = 6
m2(5 + a) // Compiler error here
}

def m2( x : A) = {
x.toString
}

}

The error is:


test/B.scala:7: error: overloaded method value + with alternatives (Int)Int <and> (Char)Int <and> (Short)Int <and> (Byte)Int cannot be applied to (test.A)
m2(5 + a)
^
one error found


From reading Chapter 6 (and ahead some in Chapter 21), I expected that I would be able to invoke the + operator and m() on integers since I have the implicit convert method in the companion object to A.

What am I missing?

BTW I'm using:
Scala compiler version 2.7.3.final -- Copyright 2002-2009, LAMP/EPFL

Thanks,

Paul


Alex Cruise

Posts: 3
Nickname: arrgh
Registered: Dec, 2007

Re: Question about implicit conversions Posted: Apr 30, 2009 6:07 PM
Reply to this message Reply
I believe this thread is related to your question.

Specifically, try annotating the conversion method with a result type.

Paul Carter

Posts: 2
Nickname: 65998
Registered: Apr, 2009

Re: Question about implicit conversions Posted: May 4, 2009 7:03 AM
Reply to this message Reply
> I believe
> this thread is related to
> your question.
>
> Specifically, try annotating the conversion method with a
> result type.

Thanks for your reply, but changing object A's defn to:

object A {
implicit def convert( x: Int) : A = {
println("convert")
new A(x)
}
}


had no effect. I get the same error compiling B.scala

--
Paul

Sergei Drannikov

Posts: 2
Nickname: assedr
Registered: Mar, 2009

Re: Question about implicit conversions Posted: May 18, 2009 10:18 PM
Reply to this message Reply
Hi.

My guess:

I think implicit conversion works only if the compiler cannot find the desirable method on the would-be-converted type. So if you change the name of the operation from "+" to say "$" (there's no operation "$" on integers) in class A (and correspondingly in B - "5 $ a"), your code will work (if you also don't forget to include "import A._;" in B).

Or else, if you just swap the values in B - "a + 5" instead of "5 + a", your code will again compile.

Flat View: This topic has 3 replies on 1 page
Topic: XML Binding Previous Topic   Next Topic Topic: Run class in interpreter??

Sponsored Links



Google
  Web Artima.com   

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