Reputation: 967
I am reasonable new to scala and working with scala and Java together.
I am trying to pass a scala Int into a method that accepts an Integer(java.long.Integer). Since they are of different types the compiler gives an error.
/* name- Option[String], id- Option[Integer] , mask- Option[String]*/
new findingFrame(name,id, mask)
case class findingFrame(name: String,
id: Option[java.lang.Integer],
mask : Option[String])
I tried using .instanceOf [java.lang.Integer], but this doesn't work either..
I am not sure how to solve this.. Can somebody help me please? Thank you.
Upvotes: 13
Views: 18165
Reputation: 1294
If automatic conversion didn't work for you (for example, if you have some intermediate steps and type checker can't immediately find what should be the final type) you can use int2Integer
(or long2Long
for Long) from scala.language.implicitConversions
which is imported by default (https://docs.scala-lang.org/tour/implicit-conversions.html)
Upvotes: 0
Reputation: 247
I think most other answers cover it, your issue is with the use of Option, not with the difference between scala and java integers. There's many things you can do with options, for example: Given:
val optionOfInteger = Option(5)
val i = optionOfInteger.get
)val i = optionOfInteger.getOrElse(0)
)val optionOfString = optionOfInteger.map(_.toString)
)val str = optionOfInteger.fold("Nothing")(_.toString)
)def myFn(findingFrame: FindingFrame) = {
findingFrame.in.foreach{i => javaMethod(i) }
}
In the above you could use map, or match/case instead.
Upvotes: 2
Reputation: 99
All you need to do is pass the int value to the constructor of Integer(). You can pass this new object now.
scala> val n = 20
n: Int = 20
scala> new Integer(n)
res0: Integer = 20
scala> res0.getClass
res1: Class[_ <: Integer] = class java.lang.Integer
Upvotes: 8
Reputation: 4753
scala> val i: java.lang.Integer = 1
i: Integer = 1
scala> val id: Option[java.lang.Integer] = Some(i)
id: Option[Integer] = Some(1)
scala> id.get.getClass
res9: Class[_ <: Integer] = class java.lang.Integer
Upvotes: 1
Reputation: 4925
Scala using box for primitive types, it can pass directly to Java.
In the following code, type i is actually int
, and int
can passed to Random which requires long
since int
will convert to long
automatically.
val i = 3
println(i.getClass)
println(new Random(i))
Int the following code, using Option[Integer]
, Option[Integer].get
will get the value inside which is type Integer
. And Integer
can not passed directly to long
, while we can use .toLong
to convert it to Long
which will automatically unbox to long
.
val oi: Option[Integer] = new Some(3)
println(oi.getClass)
println(new Random(oi.get.toLong))
Upvotes: 2
Reputation: 174
I think your problem is not so much about scala.Int vs. java.lang.Integer but the use of Option types.
Short answer: this seems to work for me val i: Int = 7 new findingFrame("foo", Some(i), None)
Long answer: The scala Option types are wrappers that may or may not contain a value of its generic type. The idea is to avoid Java's null values. An Option may be either None (does not contain a value) or Some[T] (does contain a value) - None and Some are subclasses of Option here. So you just need to wrap your scala Int into a Some as in the code above.
Upvotes: 2