score:3
When you don't supply a type signature but one is needed, Scala uses the most restrictive signature possible. Since Nothing
is the most restrictive of all (nothing can be Nothing
!), Scala chooses LinkedBlockingQueue[Nothing]
.
But in this case, the restrictiveness means that you can't actually put anything into this highly-restrictive queue.
As you've already discovered, the solution is to specify the type of classes in the collection:
val queue = new LinkedBlockingQueue[String]
But note that the type inferencer can figure out the right type in other cases by following the "as restrictive as possible" rule. For example if initial
is another Java collection that is typed as containing strings,
val queue = new LinkedBlockingQueue(initial)
would just work, as it would read off the String
type from initial
.
score:1
I guessed right. The type must be specified as shown -
val queue=new LinkedBlockingQueue[String]
score:1
Java raw types are a concession to backward compatibility and their usage considered bad style. In Scala you don't have raw types, so you must either specify them or the type inferencer must be able to infer them. Note that you can specify the parametric type on the left side, too:
val queue:LinkedBlockingQueue[String] = new LinkedBlockingQueue()
That makes here not much sense, but is important if you want to have a different type (e.g. Queue[String]
for your variable.
score:2
For concurrent collection you can use the google collections (Guava) wich has a GenericMapMaker factory.
Source: stackoverflow.com
Related Query
- java.util.concurrent.LinkedBlockingQueue put method requires Nothing as argument in Scala
- Making Scala choose less specific overloaded method in presence of argument of type Nothing
- Why does scalac take the Java vararg method instead of the single argument
- Pass Map keys as argument to Java varags method
- Wrapping null-returning method in Java with Option in Scala?
- Is it possible to use a Java 8 style method references in Scala?
- What does :_* do when calling a Java vararg method from Scala?
- Can I create a method with Java protected access in Scala?
- How to define a method in Scala that returns a Java object?
- If the Nothing type is at the bottom of the class hierarchy, why can I not call any conceivable method on it?
- Put method in trait or in case class?
- What advantages does Scala have over Java for concurrent programming?
- How do I call a Java method named the same as a Scala keyword?
- How to call T eq(Object) method of Java interface from Scala?
- How to call main method of a Scala program from the main method of a java program?
- Why does Scala evaluate the argument for a call-by-name parameter if the method is infix and right-associative?
- How to access a Java static method from Scala given a type alias for that class it resides in
- Calling Java vararg method from Scala with primitives
- deprecation warning when compiling: eta expansion of zero argument method
- How to implement Scala apply method in Java
- How to add a factory method to an existing Java class in Scala
- How to declare scala method so that it can be called from Java using varargs style
- How to get Scala annotations that are given to an argument of a method
- Calling a protected static Java method from Scala
- scala : it is impossible to put a tuple as a function's argument
- Why can't _ be used to indicate an unused/ignored argument in a method override?
- Calling an overloaded java generic method from scala
- How do you get java method annotations to work in scala
- Method References like in Java 8 in Scala
- Scala: Overwriting a Generic Java Method that returns null
More Query from same tag
- Variables documentation in Scala
- spark dataset overwrite particular partition not working in spark 2.4
- Akka persist on recovery completed updates state after first message
- Lazy evaluation steps : filtering a list
- What does ()=> mean in Scala?
- Exclude latest version of dependent projects in Scala
- Slick 2.0 - update two or more columns
- How to fix PolymorphicExpression error on upper bound type
- Removing SBT projects and removing Typesafe Stack
- Can't seem to get Future to run callback in Scala
- What's the meaning of num_slices parameter in sc.parallelize?
- scala, string variable processed as string not regexp
- Scala higher-kinded existential type
- Stream Eventhub Fixed Length data to a streaming DataFrame
- Running in issues after running Spark/scala job with single case id
- Scala return Object type for Map method
- How to create elasticsearch aggregation using Java API on scala
- Collecting all identifers in scope
- Can I specify constraints on type members of type parameters?
- Running a jar produce from gradle build
- How to get the latest offset of kafka
- What kinds of functions are considered as "composable"?
- Handling errors when combining multiple WS calls into one result
- Where can I set proxy for SBT in Intellij IDEA?
- DataType verification on DataFrame Scala
- Response in akka using ask pattern
- java.lang.ClassNotFoundException: org.apache.spark.sql.DataFrame error when running Scala MongoDB connector
- Iterate through Files in Google Cloud Bucket
- Scala file symbols in Intellij
- Spark word count example : why error: value split is not a member of Char at the REPL?