Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beginner: Scala type alias in Scala 2.10?

Tags:

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


}
like image 368
Tony Avatar asked Apr 03 '13 09:04

Tony


2 Answers

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)
      )
}
like image 129
Régis Jean-Gilles Avatar answered Sep 19 '22 05:09

Régis Jean-Gilles


From this article you have.

Note also that along with most of the type aliases in package scala comes a value alias of the same name. For instance, there's a type alias for the List class and a value alias for the List object.

A solution to the problem translates to:

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))
}
like image 30
korefn Avatar answered Sep 18 '22 05:09

korefn