Beginner: Scala type alias in Scala 2.10?

后端 未结 2 1052
-上瘾入骨i
-上瘾入骨i 2021-02-02 13:13

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?



        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-02 13:19

    Matrix denotes a type, but you are using it as a value.

    When you do List(1, 2, 3), you are actually calling List.apply, which is a factory method for List.

    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)
          )
    }
    

提交回复
热议问题