score:8
you have to constrain this
to be a superclass of null
- which is the way of telling the compiler that null
is a valid value for that type. (in fact, thinking about any
, anyref
and anyval
only muddles the problem - just ask the compiler for what you want!)
trait link[this >: null] {
var next:this = null
}
however, i would suggest that you avoid using null
, you could use option[this]
and affect none
- such a construction will allow you to use pattern matching, and is a very strong statement that clients using this field should expect it to maybe have no value.
trait link[this] {
var next:option[this] = none
}
score:0
trait link {
var next:this = null
}
this should work. is there a specific reason that you want to need to parameterize the trait with a type?
score:0
my first thought, which didn't work. i'm not sure why.
trait link[this <: anyref] { // without the type bound, it's any
var next: this = null
}
when worse comes to worst, there's always casting:
trait link[this <: anyref] {
var next: this = null.asinstanceof[this]
}
with the cast, you no longer need the type bound for this trait to compile, although you might want it there for other reasons.
Source: stackoverflow.com
Related Query
- What does a type have to be constrained to before it can be assigned "null"?
- What special rules does the scala compiler have for the unit type within the type system
- What return type should a Scala method have if it can throw/return errors but has Unit return type?
- Spark can not write delta table if the directory does not exits before (does not have _delta_log)
- What is the efficient way to create Spark DataFrame in Scala with array type columns from another DataFrame that does not have an array column?
- What does it mean to have an 'empty' case statement in Scala?
- What does "reflective access of structural type member method should be enabled..." warning mean in Scala?
- What advantages does Ceylon have over Java or Scala
- What advantages does Scala have over Java for concurrent programming?
- How can I see in what [Java/Scala?] code does Scala compiler rewrites original Scala-code
- What does "no global type inference" mean regarding Scala?
- Why does scala.util.Failure have a type parameter?
- What does Kotlin's type reification make possible that is not possible in Java or Scala?
- Why can I assign null to a Unit value and why does it get converted to ()?
- How can I write a function have a polymorphic return type based on the type argument of its type parameter?
- What advantages does scala.util.Try have over try..catch?
- Why does fold have the following type in Scala?
- What does Dotty offer to replace type projections?
- Why does this code typecheck in Scala 2.11 and what can I do about it?
- What kind of impact does applying all these Scala traits have at runtime?
- How does CanBuildFrom know whether a type can build from another?
- What does '#' mean in a type parameter?
- What does the new scala Dynamic type do?
- What type does function has?
- Why can method parameter F have the same name as type constructor F?
- What do I need to learn before I can start using Lift?
- How can I have a negation type in Scala?
- Why Scala doesn't have enum type and what are benefits of current design?
- Why does Scalaz \/ type have getOrElse but not get?
- Can an existentially quantified type variable be forced to have only a single type?
More Query from same tag
- Can't access extra methods of trait implementation
- How to pass Any to a generic method?
- How to pivot on more than one column for a spark dataframe?
- What exactly is a "Null reply" in Redis?
- Can a single class cause IncompatibleClassChangeError with itself?
- How can I convert a unicode value to it's representation?
- Amazon Emr: Unable to increase default memory settings
- Practicing with Spark's join and scala on Array[String]
- How use recursive type T=mutable.Map[Char,T] in Scala
- How can we convert Hex string to Byte, Short datatype in scala
- Matching a sequence within a sequence in scala
- Why does the andThen of Future not chain the result?
- Akka remote : trying to connect two machines
- Scala Lazy Val Question
- What is an elegant way to check if a character is an ASCII letter (a-Z) in Scala?
- Kleisli dependencies with Tagless Final style
- find line number in an unstructured file in scala
- How to deal with infinite Future in Scala/Akka?
- Scala: Contains in mutable and immutable sets
- Eliminating identity wrapper types from Scala APIs
- Implementing user access to multiple Play instances
- too many arguments for method apply: (i: Int)String in class Array
- Why in scala a comparison between integer and floating point such as 71 == 71.0 is true?
- How to exclude scala formatting for html files using vs code
- Spark - Best way agg two values using ReduceByKey
- How can I pass a play template scala variable as input to a scala function?
- Best way to turn a Lists of Eithers into an Either of Lists?
- How to build Encoder for Regex class in Scala w/ Spark
- How do I reference a config file in Scala WorkSheet and Akka?
- Defining an array in play's config file