Question

I am studying scala and slick. And I got an error like this:

found :   Option[Int]
required: Int
  def update(c: Color): Int = findById(c.id).update(c)

I am not sure what is found and required stand for. So I add other functions:

def update(c: Color): Int          = findById(c.id).update(c)
def update2(c: Color): Option[Int] = findById(c.id).update(c)
def update3(c: Color): String      = findById(c.id).update(c)
def update4(c: Color): Unit        = findById(c.id).update(c)

And expecting different found and required, but same error came out:

found :   Option[Int]
required: Int
  def update(c: Color): Int = findById(c.id).update(c)

found :   Option[Int]
required: Int
  def update2(c: Color): Option[Int] = findById(c.id).update(c)

found :   Option[Int]
required: Int
  def update3(c: Color): String      = findById(c.id).update(c)

found :   Option[Int]
required: Int
  def update4(c: Color): Unit        = findById(c.id).update(c)

Why same error came out? What is found and required stand for? Thanks.

Was it helpful?

Solution

The source of the error message is this part of your code

findById(c.id)

This part is the same for all four examples given. Thus the same error message.

findById( id ) expects an Int as an argument but c.id returns an Option[Int].

A possible solution would be to map over c.id

c.id map ( id => findById(id) update c  )

then it would return an Option[Int] as required by your update2.

Or you could use a for comprehension which would return an Int as your function update requires.

for {
  id <- c.id
  elem = findBy(id)
} yield elem.update(c)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top