这必须成为改进的体现,因为以下两条表述都是工作。
object Qx1 {
// give length() parens, even though the Iterator definition doesn t have them
type HasLength = { def length(): Int }
def fn(xs: Iterator[HasLength]) = 3
var tn = fn(Iterator("foo"))
var fooIterator = Iterator("foo")
var tnFails = fn(fooIterator) //doesn t compile
}
object Qx2 {
type HasLength = { def length: Int }
def fn(xs: Iterator[HasLength]) = 3
var tn = fn(Iterator("foo"))
// annotate the type of fooIterator before the type inferencer can mis-infer
var fooIterator: Iterator[HasLength] = Iterator("foo")
var tnFails = fn(fooIterator) //doesn t compile
}
Edit:
上午太早。 这套方法有长处,这意味着它正确,而且你在思考时间长度和时间长度方面有错误。 (这只剩下一个小小小小块的陷阱,在前有记载。)