dam*_*uar 2 polymorphism types scala
我正在尝试在解释器中加载Scala文件:
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)
}
Run Code Online (Sandbox Code Playgroud)
但我收到一个愚蠢的错误:
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
Run Code Online (Sandbox Code Playgroud)
我怎样才能让翻译知道MyInt课程,这是在路上定义的?
我想你想要的行为:paste.:load表现就像你在解释器中输入一样,即,一旦找到结束括号,它就会解释.您可以:paste通过将代码包装在某个对象中来模拟,如下所示:
object Test {
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)
}
}
Run Code Online (Sandbox Code Playgroud)
现在,你可以把它作为你想后:load Test.scala和 import Test._
| 归档时间: |
|
| 查看次数: |
1141 次 |
| 最近记录: |