Scala - 如何去解决“值不是Nothing的成员”错误 [英] Scala - how to go resolve "Value is not a member of Nothing" error
问题描述
此示例代码基于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
将实际上从不返回值。
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 fromNothing
itself, you have to specify exact type ((bc: ExactType).broadcast(message)
).Nothing
has no instances. Method, that returnsNothing
will, actually, never return value. It will throw an exception eventually.Type inference
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(...) = ???
???
returnsNothing
.You should specify either result type of
lookup
method like this:def lookup(...): <TypeHere> = ...
or type ofbc
:val bc: <TypeHere> =
.这篇关于Scala - 如何去解决“值不是Nothing的成员”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!