初学者:Scala 2.10 中的 Scala 类型别名? [英] Beginner: Scala type alias in Scala 2.10?
本文介绍了初学者:Scala 2.10 中的 Scala 类型别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么此代码无法编译并显示错误:未找到:值矩阵?从文档和一些(可能已经过时的)代码示例来看,这应该可行吗?
Why does this code fail to compile with the error: not found: value Matrix? From the documentation and some (possibly out of date) code examples this should work?
object TestMatrix extends App{
type Row = List[Int]
type Matrix = List[Row]
val m = Matrix( Row(1,2,3),
Row(1,2,3),
Row(1,2,3)
)
}
推荐答案
Matrix
表示一种类型,但您将其用作值.
Matrix
denotes a type, but you are using it as a value.
当你做List(1, 2, 3)
时,你实际上是在调用List.apply
,它是List
.
When you do List(1, 2, 3)
, you are actually calling List.apply
, which is a factory method for List
.
要修复编译错误,您可以为Matrix
和Row
定义自己的工厂:
To fix your compiling error, you can define your own factories for Matrix
and Row
:
object TestMatrix extends App{
type Row = List[Int]
def Row(xs: Int*) = List(xs: _*)
type Matrix = List[Row]
def Matrix(xs: Row*) = List(xs: _*)
val m = Matrix( Row(1,2,3),
Row(1,2,3),
Row(1,2,3)
)
}
这篇关于初学者:Scala 2.10 中的 Scala 类型别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文