score:1
As said in comments, this solution doesn't work
If I understood your problem well, you can also assign default values, as explained in details in what does it mean assign "_" to a field in scala?. You can find more infos in 4.2 Variable Declarations and Definitions of the The Scala Language Specification
So you can simply do:
def unwrap[T](iface: Class[T]): T = _
which will set unwrap
with null
without the type mismatch.
score:6
The "correct" solution is to do something which will immediately fail. Like so:
def unwrap[T](iface: Class[T]): T = sys.error("unimplemented")
In scala 2.10, this would have been implemented as:
def unwrap[T](iface: Class[T]): T = ???
Because there is a new method in Predef
called ???
. This works because an expression of the form throw new Exception
has the type Nothing
, which is a subtype of any type (it is called bottom in type-theoretical circles).
The reason that this is correct is that it is much better to fail instantly with an error, rather than use a null
which may fail later and obfuscate the cause.
score:26
This is because T
could be a non-nullable type. It works when you enforce T
to be a nullable type:
def unwrap[T >: Null](iface: Class[T]): T = null
unwrap(classOf[String]) // compiles
unwrap(classOf[Int]) // does not compile, because Int is not nullable
Source: stackoverflow.com
Related Query
- How to return null from a generic function in Scala?
- How to return correct type from generic function passed a related abstract type parameter
- How to assign a function as a return type from function in Scala
- How can I call a Scala function taking a PartialFunction with a Unit return type from Java?
- How are Scala collections able to return the correct collection type from a map operation?
- How do I implement a generic mathematical function in Scala
- How to return a function in scala
- How to turn off Scala auto-completion of function with Unit return type in IntelliJ IDEA?
- How do I create a generic Scala function that returns an object based on the generic type?
- scala generic function return type
- Return copy of case class from generic function without runtime cast
- How to return Unit from a scala function?
- How does Scala implement return from within an expression?
- How to return multiple random elements from List scala
- How to define a function does not return or return void in scala
- How to call function from hashmap in Scala
- Using IntelliJ, how can i determine whether particular function stems from Java or Scala
- How to return different results from Futures in Scala without ending up in nested if statements
- How to best implement "first success" in Scala (i.e., return the first success from a sequence of failure-prone operations)
- How to call a scala function from php?
- How to optionally return value from map function
- Calling java generic function from scala
- How to implement scala generic function that takes subtype of parameterized traits and returns it
- How to create partial function from runtime value in Scala
- How to return Scala `Unit` type in java function
- How to instantiate a nested generic class defined in scala from java?
- How to write generic function with Scala Quill.io library
- How to get the set of rows which contains null values from dataframe in scala using filter
- How to avoid dropping null values from dropduplicate function when passing single column
- How to implement generic function in Scala with two argument types?
More Query from same tag
- get ServerStatus using Scala/Play Framework
- Spark ERROR executor: Exception in task 0.0 in stage 0.0 (tid 0) java.lang.ArithmeticException
- Scala: Is there a way to use PriorityQueue like I would in Java?
- passing Dataframe contents into sql stored procedure
- Trying to get all records from 2 weeks ago
- sbt-native-packager docker:publishLocal returns: Nonzero exit value: 125
- Scala: using value field to implement a trait method
- Scala: type-based list partitioning
- initialization of two vals depending on each other in scala Intellij worksheet?
- Google Ads API dependencies with SBT
- Why it returns 302 and no error message returns with `If` in `Menu`?
- Use Spark fileoutputcommitter.algorithm.version=2 with AWS Glue
- Can someone explain the scala code below?
- playframework owasp top 10
- How to provide a text file location in spark when file is on server
- Breakpoints from Scala Worksheet?
- Embedded Cassandra with Spark and DataStax Driver Error
- Spark Scala UDF NullPointer Exception
- oop -- mutual dependency between chessboard and its pieces
- Guice in Scala: Module for a class that has a DI-constructor itself
- Calling a function periodically in Scala while another expensive function is computing
- Spark/Scala flatten and flatMap is not working on DataFrame
- SBT - Adding custom resolver is not working
- Running Scala Spark applications on Mac
- Calculate table statistics using scala and spark-sql
- How to access individual value of a map in a Map[String,(String, String)]
- representation of values (x,y) vs x._1,y._1
- Can I use a ES client that creates a local node and joins a cluster over TCP on Heroku?
- Spark: How to collect a single column from an ArrayType of columns to a different array?
- Create Spark Dataframe from SQL Query