Support Questions

Find answers, ask questions, and share your expertise

Scala Implicit Conversion

avatar
Rising Star

Hi,

I'm new to Scala and learning now. I'm trying a program with implicit conversions. But, it shows error in the main program.

I've attached the program file for your reference. Please note that both the Rational class and Rational main object in the same project. So, i don't have to import the Rational class to the main program. I'm trying these programs in Scala eclipse IDE. Please let me know your information on this.. Thanks in advance.

Regards,

Jeeva

implicit-error.txt

1 ACCEPTED SOLUTION

avatar

intToRational is only in the scope of Rational in your code, so the conversion isn't available to Ints outside of the Rational class -- the reverse order (x + 2) works because 2 ends up being bound by the + inside Rational, where the conversion is available. What you want to do is create a companion Rational object, define intToRational there, and then you can import it in Rational, and outside of it (global scope, e.g.) too.

As a minor note, I'd check out the Spire project for a complete set of rational classes, plus a whole lot more.

View solution in original post

5 REPLIES 5

avatar

intToRational is only in the scope of Rational in your code, so the conversion isn't available to Ints outside of the Rational class -- the reverse order (x + 2) works because 2 ends up being bound by the + inside Rational, where the conversion is available. What you want to do is create a companion Rational object, define intToRational there, and then you can import it in Rational, and outside of it (global scope, e.g.) too.

As a minor note, I'd check out the Spire project for a complete set of rational classes, plus a whole lot more.

avatar
Rising Star

Dear jfrazee,

Thank you very much for your prompt reply. Somehow i figured out the answer in different way by defining the implicit function within the object where the main function is , but outside the main function. But, I'll try your approach too.. Once again Thank you very much. I appreciate your support..

avatar

Not sure what happened to the comment I made on doing this, so re-posting it. Part of this is about code-style. You generally don't want to define implicits at the top-level because it can make the code more difficult to reason about. For this reason it's common to tuck the implicits into a companion object (e.g., the relevant class or an Implicits object) and then import them just where you need them. This is probably the best use case for being able to do imports in the scope of a class, object or function -- you can apply an implicit without polluting the whole space.

avatar
Rising Star

I tried the implicit function in the way as you suggested and it works very well. Thank you for explaining why the implicit functions are kept in Companion Object classes. You have done very good job...

One more request is that, Could you please send me any shell script if you have to automate sqoop incremental import job?

Once again Thank you very much...!!!

avatar

@Jeeva Jeeva You're probably best off posting that as another question (both to get it answered and so it's more searchable). I don't have anything in hand at the moment. Best.