I’m trying to load a Scala file inside the interpreter:
trait MyOrdered {
def <(that: MyInt):Boolean = compare(that) < 0
def >(that: MyInt):Boolean = compare(that) > 0
def <=(that: MyInt):Boolean = compare(that) < 0 || this == that
def >=(that: MyInt):Boolean = compare(that) > 0 || this == that
def compare(that: MyInt): Int
}
class MyInt(val value: Int) extends MyOrdered {
def compare(that: MyInt) =
if (this.value < that.value) -1
else if (this.value == that.value) 0
else 1
}
object App extends Application{
val a = new MyInt(2)
val b = new MyInt(4)
println(a < b)
println(a > b)
}
But I receive a silly error:
Loading traits.scala...
<console>:8: error: not found: type MyInt
def <(that: MyInt):Boolean = compare(that) < 0
^
<console>:12: error: not found: type MyInt
def compare(that: MyInt): Int
How can I make the interpreter know the MyInt class, which is defined down the road?
I think you want the behavior of
:paste.:loadbehaves as if you are typing in the interpreter i.e., it interprets as soon as it finds the closing braces. You can emulate the:pasteby wrapping your code in some object like this:Now you can use it as you want after
:load Test.scalaandimport Test._