Scala - How to solve the "Value is not a member of Nothing" problem

This sample code is based on Atmosphere classes, but if someone can give me some idea of ​​what the error as a whole means, I think I can figure out any solution based on the atmosphere ...

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

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

Any ideas / suggestions as to what might trigger this?

Thanks.

EDIT: Signature for [BroadcasterFactor] .lookup: Abstract Broadcast Search (Object ID)

Note: 1) this is the version of the signature that I used in the example, 2) this is the signature of java Inteface, while getDefault () returns an instance of an object that implements this interface.

Solution: type of pushing the value:

 val bc: Broadcaster = BroadcasterFactory.getDefault().lookup(_broadcasterId) 
+4
source share
1 answer

Nothing is the type name. This is a subtype of all other types. You cannot call methods from Nothing , you need to specify the exact type ( (bc: ExactType).broadcast(message) ). Nothing has no instances. The method that returns Nothing will never actually return a value. This will ultimately lead to an exception.

Type inference

Definition of lookup :

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

in scala, this definition is as follows:

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

There is no type parameter specified in the lookup method. In this case, the compiler will output this type of 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 can specify a type parameter as follows:

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

Implementation project

During development, a lookup can be "implemented" as follows:

 def lookup(...) = ??? 

??? returns Nothing .

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

+4
source

Source: https://habr.com/ru/post/1494881/


All Articles