0

Why does not this work? In a Java code:

import scala.collections.immutable.Range;

// ...

Range r = Range.apply(0, 10)

Eclipse says:

The method apply(int) in the type Range is not applicable for the arguments (int, int)

And SBT says:

error: method apply in class Range cannot be applied to given types;

However, there is an apply(Int, Int) method in the collections.immutable.Range object of the Scala API.

scand1sk
  • 1,114
  • 11
  • 25

1 Answers1

3

That's because you're calling the apply(int) method from the Range class. You should be calling apply(int,int) from the companion object:

import scala.collection.immutable.Range$;
// ...
Range r = Range$.MODULE$.apply(0, 10)

See also this Q&A for general info.

Community
  • 1
  • 1
mikołak
  • 9,605
  • 1
  • 48
  • 70
  • 4
    Yeah, but normally scalac will generate static forwarders for companion object methods. In this case however, it cannot, because `Range` class already contains a method named `apply` (regardless of the fact that it has different signature). – ghik Mar 09 '14 at 23:14
  • 1
    More details on why static forwarder is not generated: https://groups.google.com/forum/#!topic/scala-internals/FeXSYcAv2ho – ghik Mar 09 '14 at 23:18
  • Thanks, I will just add a Java interop object to my API/DSL to circumvent this. I don't want my users to write such things as `Range$.MODULE$.apply` all over the place. – scand1sk Mar 10 '14 at 14:13