Still have error “value copy is not a member of Nothing” even I filter Nothing class

狂风中的少年 提交于 2019-12-11 08:35:24

问题


I use scala 2.11.2. This is part of my function:

import scala.reflect.runtime.universe._
p => p.filter(p => typeOf[p.type] != typeOf[Nothing]).flatMap {
    case Some(profile) => {
        ...
        env.userService.save(profile.copy(passwordInfo = Some(hashed)),...) //<---------error here
    }
    case _ => ...
}

the compile error is:

PasswordReset.scala:120: value copy is not a member of Nothing
[error]                   env.userService.save(profile.copy(passwordI
nfo = Some(hashed)), SaveMode.PasswordChange);
[error]                                                ^

I think I use filter phase filter the Nothing type, but why it is still give me type Nothing error. I do not want to:

profile.getDefault().copy(...)

Because I really need to copy the profile instead of to copy the default value, if profile is Nothing just delete it. How to do it?


回答1:


Filter doesn't change types.

scala> def f[A](x: Option[A]) = x filter (_ != null)
f: [A](x: Option[A])Option[A]

Option[A] comes in, Option[A] goes out.

You're suggesting that the runtime check in your filter function should instruct the compiler to accept that your type param is not Nothing, but that's not how it works.

scala> f(None)
res2: Option[Nothing] = None

If Nothing is inferred, then Nothing is what you get.

You want to keep the compiler from inferring Nothing somewhere. Sometimes it's necessary to provide explicit type args to do that:

scala> f[String](None)
res3: Option[String] = None

scala> f[String](None) map (_.length)
res4: Option[Int] = None

compare to

scala> f(None) map (_.length)
<console>:9: error: value length is not a member of Nothing
              f(None) map (_.length)
                             ^

But it's also possible you could express your code differently.



来源:https://stackoverflow.com/questions/25163255/still-have-error-value-copy-is-not-a-member-of-nothing-even-i-filter-nothing-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!