为什么这段代码无法使用错误编译:找不到:值矩阵?从文档和一些(可能是过时的)代码示例,这应该工作?
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)
)
}
Run Code Online (Sandbox Code Playgroud)
Rég*_*les 49
Matrix 表示类型,但您将其用作值.
当你这样做时List(1, 2, 3),你实际上正在调用List.apply,这是一种工厂方法List.
为了解决您的编译错误,您可以定义自己的工厂进行Matrix和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)
)
}
Run Code Online (Sandbox Code Playgroud)
从这个文章你.
另请注意,scala包中的大多数类型别名都带有同名的别名.例如,List类的类型别名和List对象的值别名.
该问题的解决方案转化为:
object TestMatrix extends App{
type Row = List[Int]
val Row = List
type Matrix = List[Row]
val Matrix = List
val m = Matrix( Row(1,2,3),
Row(1,2,3),
Row(1,2,3))
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12400 次 |
| 最近记录: |