0

I have java code like compiles fine.

import org.jaitools.numeric.Range;
Range<Integer> r1 = Range.create(1, true, 4, true);

Converted to Scala like

val r1: org.jaitools.numeric.Range[Integer] = org.jaitools.numeric.Range.create(1, true, 4, true)

compilation fails as java seems to go for this method:

public static <T extends Number & Comparable> Range<T> create(T minValue, boolean minIncluded, T maxValue, boolean maxIncluded) {
        return new Range<T>(minValue, minIncluded, maxValue, maxIncluded);
    }

whereas the Scala compiler will choose to use

public static <T extends Number & Comparable> Range<T> create(T value, int... inf) {
        return new Range<T>(value, inf);
}

i.e. the type arguments do not match.

Both are overloaded methods in the same class. How can I get the Scala compiler to choose the right method?

edit

val r1: org.jaitools.numeric.Range[Integer] = org.jaitools.numeric.Range.create(1, true, 4, true)

results in

overloaded method value create with alternatives:
  [T <: Number with Comparable[_]](x$1: T, x$2: Int*)org.jaitools.numeric.Range[T] <and>
  [T <: Number with Comparable[_]](x$1: T, x$2: Boolean, x$3: T, x$4: Boolean)org.jaitools.numeric.Range[T]
 cannot be applied to (Int, Boolean, Int, Boolean)
       val r1: org.jaitools.numeric.Range[Integer] = org.jaitools.numeric.Range.create(1, true, 4, true)

Maybe this is also a case of convert java to scala code - change of method signatures where the type system of java and Scala do not work well together?

7
  • I am not familiar with this specific package but this seems to be an issue of converting types. try to do Range.create(int2Int(1), true, int2Int(4), true); Commented Jun 2, 2017 at 15:53
  • Which method are you referring to with int2Int ? Commented Jun 2, 2017 at 15:55
  • Note that you are using Range[Integer] which refers to java.util.Integer, however in scala 1 and 4 are Int. scala predef includes a function int2Int to do the conversion. While this generally happens automatically, it can fail in certain situations. This could be one of them Commented Jun 2, 2017 at 15:57
  • In the scala gitter I was told to try org.jaitools.numeric.Range.create(Integer.valueOf(1), true, Integer.valueOf(4), true) i.e. to use manual boxing. This works fine. Commented Jun 2, 2017 at 16:02
  • It is similar logic. Another option which MIGHT work (but probably wont) is to use org.jaitools.numeric.Range[Int] Commented Jun 2, 2017 at 16:04

1 Answer 1

2

Your problem is that Int and java.lang.Integer are two different things. create expects its first and third parameters to be of the same type as the type parameter. You are specifying the param as Integer but arguments you are passing in - 1 and 4 - are of type Int.

You cannot create Range[Int], because the type parameter is required to extend Number and Comparable, which Int does not. So, you have to wrap your Ints into Integer explicitly

val r1 = org.jaitools.numeric.Range.create(Integer.valueOf(1), true, Integer.valueOf(4), true)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.