Sasha Tsukanov
Sasha Tsukanov

Reputation: 1125

Cannot use primary constructor in Scala method: type "does not take parameters"

I've got the following definition of Board:

package board

import scala.collection.immutable.Vector

class Board private (values: Vector[Vector[Cell]]) {
  private val cells = values

  def updated(i: Int, j: Int)(newValue: Cell): Board =
    this(
      cells.updated(
        i, cells(i).updated(j, newValue)
      )
    )
}

where Cell is

case class Cell(value: Option[Int])

Inside updated function at line this( I've got an error

board.Board does not take parameters

which seems odd since it definitely does. Why is this happening?

P.S. in reality my class aims to be more useful and contains a public constructor, but the above is a MCVE to get the same error

Upvotes: 1

Views: 391

Answers (1)

Sasha Tsukanov
Sasha Tsukanov

Reputation: 1125

The problem was that I called this outside of a constructor. I needed to change it to new Board:

def updated(i: Int, j: Int)(newValue: Cell): Board =
  new Board(
    cells.updated(
      i, cells(i).updated(j, newValue)
    )
  )

Upvotes: 1

Related Questions