Scala - 如何去解决“值不是Nothing的成员”错误 [英] Scala - how to go resolve "Value is not a member of Nothing" error

查看:467
本文介绍了Scala - 如何去解决“值不是Nothing的成员”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此示例代码基于Atmosphere类,但如果有人可以给我一些错误的意义,我想我可以找出任何大气特定的解决方案...

  val bc = BroadcasterFactory.getDefault()。lookup(_broadcasterId)
bc.broadcast(message)



在第一行之后,bc应该包含一个对象的句柄,其类定义包括方法broadcast() - 实际上,它包含几个重载的变体。但是,编译器使用以下代码阻塞第二行代码:value broadcast不是Nothing的成员



任何关于会导致什么的想法/建议?



谢谢。



编辑:[BroadcasterFactor]的签名.lookup:
抽象广播者查找(Object id)



注意:1)这是我在示例中使用的签名版本,2)它是java Inteface签名 - 而getDefault()回传实例化的对象实现该接口。



解决方案:强制类型转换为值:

  val bc: Broadcaster = BroadcasterFactory.getDefault()。lookup(_broadcasterId)


解决方案

Nothing 是类型名称。它是所有其他类型的子类型。你不能从本身调用方法,你必须指定确切的类型((bc:ExactType).broadcast(message))。 没有任何没有实例。方法,返回 Nothing 将实际上从不返回值。



https://github.com/Atmosphere/atmosphere/blob/master/modules/cpr/src/main/java/org/atmosphere/cpr/BroadcasterFactory.java#L146 =nofollow> <$ c的定义$ c> lookup

  abstract public< T extends Broadcaster> T lookup(Object id);在scala中,

这个定义看起来是这样的:

  def lookup [T< ;: Broadcaster](Object id):T 

lookup 方法中没有指定类型参数。在这种情况下,编译器会将此类型参数推断为最具体的类型 - Nothing

  scala> def test [T](i:Int):T = ??? 
test:[T](i:Int)T

scala> lazy val x = test(1)
x:Nothing =< lazy>

scala> lazy val x = test [String](1)
x:String =< lazy>

您可以像这样指定类型参数:

  val bc = BroadcasterFactory.getDefault()。lookup [Broadcaster](_ broadcasterId)


b $ b

实施草案



在开发过程 lookup 实现像这样:

  def lookup(...)=? 

c> Nothing 。



您应指定 lookup 方法的结果类型: def lookup(...):< TypeHere> = ... 或类型 bc val bc:< TypeHere> =


This example code is based on Atmosphere classes, but if someone could give me some insights into what the error means in general, I think I can figure out any Atmosphere-specific solution...

val bc = BroadcasterFactory.getDefault().lookup(_broadcasterId) 
bc.broadcast(message)

After the first line, bc should contain a handle to an object whose class definition includes the method broadcast() -- in fact, it contains several overloaded variations. However, the compiler chokes on the second line of code with the following: "value broadcast is not a member of Nothing"

Any ideas/suggestions on what would be causing this?

Thanks.

EDIT: signature for [BroadcasterFactor].lookup : abstract Broadcaster lookup(Object id)

Note: 1) that is the signature version that I've used in the example, 2) it is the java Inteface signature - whereas the getDefault() hands back an instantiated object that implements that interface.

Solution: force type cast on value:

val bc: Broadcaster = BroadcasterFactory.getDefault().lookup(_broadcasterId)

解决方案

Nothing is the type name. It's the subtype of all other types. You can't call methods from Nothing itself, you have to specify exact type ((bc: ExactType).broadcast(message)). Nothing has no instances. Method, that returns Nothing will, actually, never return value. It will throw an exception eventually.

Type inference

Definition of lookup:

abstract public <T extends Broadcaster> T  lookup(Object id);

in scala this definition looks this way:

def lookup[T <: Broadcaster](Object id): T

There is not specified type parameter in lookup method. In this case compiler will infer this type parameter as the most specific type - Nothing:

scala> def test[T](i: Int): T = ???
test: [T](i: Int)T

scala> lazy val x = test(1)
x: Nothing = <lazy>                                                                                                                                            

scala> lazy val x = test[String](1)                                                                                                                            
x: String = <lazy>

You could specify type parameter like this:

val bc = BroadcasterFactory.getDefault().lookup[Broadcaster](_broadcasterId) 

Draft implementation

In development process lookup can be "implemented" like this:

def lookup(...) = ???

??? returns Nothing.

You should specify either result type of lookup method like this: def lookup(...): <TypeHere> = ... or type of bc: val bc: <TypeHere> =.

这篇关于Scala - 如何去解决“值不是Nothing的成员”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆