Using Scala option in Java

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Using Scala option in Java

Carfield Yim
Hi all

I just create a library in Scala with a data object have following variable:

      var price: Option[Float] = None

In Scala I can update the value using this line of code:

      object.price = Some(floatValue);

However, how can I update the value if I use that library in Java project? I am now compiling that with Scala 2.11.4, will it easier if I compile it with 2.12.x version?

Thanks!

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using Scala option in Java

Lutz Huehnken

Some() can be written as Some.apply(), which Java understands. Also though what looks like a public field in Scala is really a private field with accessor methods, so you can't just assign to it in Java. I think you should be able to rewrite it to 

object.price_$eq(Some.apply(floatValue))

If you use your library from Java a lot (or want others to use it from Java), it might be worth defining a Java-ish interface, e.g. generate get/set methods with http://www.scala-lang.org/api/current/scala/beans/BeanProperty.html

My limited Java/Scala interop experience tells me that using Java classes in Scala, and converting types back and forth, is somewhat easier (to the eye, if nothing else) than using Scala classes in Java.

Hope this helps,
Lutz


P.S.: Unrelated: as your field is named price - price values can usually be easily stored as integers, and then you'll never have to worry about floating point peculiarities.



On Friday, 20 January 2017 17:44:56 UTC+1, Carfield Yim wrote:
Hi all

I just create a library in Scala with a data object have following variable:

      var price: Option[Float] = None

In Scala I can update the value using this line of code:

      object.price = Some(floatValue);

However, how can I update the value if I use that library in Java project? I am now compiling that with Scala 2.11.4, will it easier if I compile it with 2.12.x version?

Thanks!

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using Scala option in Java

Oliver Ruebenacker

     Hello,

  Java has float and java.lang.Float, which is different from scala.Float, which tells the compiler "choose float or java.lang.Float, whatever more appropriate, and convert automatically when needed.".

  Also, I don't think Java understands that None, which is Option[Nothing], is a subtype of Option[T] for any T.

     Best, Oliver

On Sat, Jan 21, 2017 at 2:37 PM, Lutz Huehnken <[hidden email]> wrote:

Some() can be written as Some.apply(), which Java understands. Also though what looks like a public field in Scala is really a private field with accessor methods, so you can't just assign to it in Java. I think you should be able to rewrite it to 

object.price_$eq(Some.apply(floatValue))

If you use your library from Java a lot (or want others to use it from Java), it might be worth defining a Java-ish interface, e.g. generate get/set methods with http://www.scala-lang.org/api/current/scala/beans/BeanProperty.html

My limited Java/Scala interop experience tells me that using Java classes in Scala, and converting types back and forth, is somewhat easier (to the eye, if nothing else) than using Scala classes in Java.

Hope this helps,
Lutz


P.S.: Unrelated: as your field is named price - price values can usually be easily stored as integers, and then you'll never have to worry about floating point peculiarities.



On Friday, 20 January 2017 17:44:56 UTC+1, Carfield Yim wrote:
Hi all

I just create a library in Scala with a data object have following variable:

      var price: Option[Float] = None

In Scala I can update the value using this line of code:

      object.price = Some(floatValue);

However, how can I update the value if I use that library in Java project? I am now compiling that with Scala 2.11.4, will it easier if I compile it with 2.12.x version?

Thanks!

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using Scala option in Java

Seth Tisue-3
On Mon, Jan 23, 2017 at 6:46 AM, Oliver Ruebenacker <[hidden email]> wrote:

     Also, I don't think Java understands that None, which is Option[Nothing], is a subtype of Option[T] for any T.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.