简体   繁体   English

四种不同的函数,具有不同的返回类型,相同的错误

[英]Four different functions with different return type, same error

I am studying scala and slick. 我正在学习scala和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. 我不确定found什么并required代表什么。 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: 并期望foundrequired不同,但出现相同的错误:

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. 谢谢。

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] . findById(id)需要一个Int作为参数,但是c.id返回一个Option [Int]

A possible solution would be to map over c.id 一种可能的解决方案是在c.id上进行映射

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

then it would return an Option[Int] as required by your update2 . 那么它将根据您的update2的要求返回Option [Int]。

Or you could use a for comprehension which would return an Int as your function update requires. 或者您可以使用for理解 ,这将在您的函数更新需要时返回一个Int值。

for {
  id <- c.id
  elem = findBy(id)
} yield elem.update(c)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在不同对象上调用相同方法时,返回类型不同 - Return type is different when invoking the same method on different objects Scala-相同但不同的类型 - Scala - same but different type 为什么应用于2个函数的部分函数以2种不同的方式执行相同的操作会返回不同的结果? - Why does a partial function applied to 2 functions that do the same thing defined in 2 different ways return a different result? 如何将不同的函数fa,fb(不同的签名,包括返回类型)作为相同的参数传递给另一个函数fc? - How to pass different functions fa, fb (different signatures, including returning type) as the same parameter into another function fc? 在相同的Scala和Java spark函数中产生不同的结果 - Different result in same Scala and Java spark functions 在scala多重继承中,如何解决具有相同签名但返回类型不同的冲突方法? - In scala multiple inheritance, how to resolve conflicting methods with same signature but different return type? 根据输入参数返回不同的类型 - Return a different type according to an input parameter getOrElse停止进程或返回不同的类类型 - `getOrElse` to stop process or return different class type 具有相同运行时类但不同静态类型的对象的不同性能 - Different performance of object with same runtime class but different static type 对同一类型使用不同的隐式值 - Use different implicit values for same type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM