I have a sample code :
class A(str: String) {
println(s"InsideCase:::$str")
}
object A {
def apply(str: String) = {
println("foobar::")
new A(str)
}
}
object b extends App {
A("kool")
}
This code works fine in Intellij. And gives me output:
foobar::
InsideCAse:::kool
But When Try to do this on REPL :
scala> :paste
// Entering paste mode (ctrl-D to finish)
class A(str: String) {
println(s"InsideCAse:::$str")
}
object A {
def apply(str: String) = {
println("foobar::")
new A(str)
}
}
// Exiting paste mode, now interpreting.
defined class A
defined object A
scala> A("kool")
It gives me the following error :
<console>:27: error: reference to A is ambiguous;
it is imported twice in the same scope by
import $line31$read.A
and import INSTANCE.A
A("kool")
What am I missing here ? If you can please explain in detail that would be helpful.
This is a Spark REPL limitation. You can either do it an old school way with an object wrapper:
object awrapper {
class A(str: String) {
println(s"InsideCAse:::$str")
}
object A {
def apply(str: String) = {
println("foobar::")
new A(str)
}
}
}
import awrapper._
or define a package with paste -raw
(Scala 2.11+):
scala> :paste -raw
// Entering paste mode (ctrl-D to finish)
package apacakage
class A(str: String) {
println(s"InsideCAse:::$str")
}
object A {
def apply(str: String) = {
println("foobar::")
new A(str)
}
}
// Exiting paste mode, now interpreting.
scala> import apacakage._
import apacakage._
scala> A("kool")
foobar::
InsideCAse:::kool
res1: apacakage.A = apacakage.A@6e818345