score:3
You are correct; the new
is not mandatory. They could have defined the instance method List#::
like this just as well:
def ::[U >: T](x: U): List[U] = scala.::(x, this)
(Note that we have:
type :: = collection.immutable.::
val :: = collection.immutable.::
defined in the scala
package object; the first is why your new scala.::(x, this)
works, and the second is why my scala.::(x, this)
works.)
The form the library uses calls the constructor directly, as yours does. The alternative calls the apply
method of the synthetic companion object generated for the ::
case class, which simply calls the constructor anyway. Perhaps calling the constructor was deemed clearer, or more efficient? (Efficiency gains should be close to nothing, though, since if the compiler doesn't inline the call to apply
, the JVM will.) I suppose the most compact form:
def ::[U >: T](x: U) = ::(x, this)
could be mistaken for some wacky (i.e., impossible) sort of recursive invocation, and at any rate blurs the distinction between the class called ::
and the List
method called ::
, which Prof. Odersky takes pains to keep separate in order to maximize reader comprehension.
Hope this helps.
score:7
With case classes you automatically get a companion object whose apply
method calls the constructor, in the same way as you can do this with an ordinary class:
class Foo(val value: Int)
object Foo { def apply(value: Int) = new Foo(value) }
val x = new Foo(42) //
val y = Foo(42) // both work the same
You can instantiate case classes with new
if you want to. It might in theory be slightly faster because it doesn't have to go via the companion object's apply
method, but I tried a quick benchmark and saw absolutely no difference in performance, so I guess it's optimised by the compiler, or just an immeasurably small difference compared to the actual construction.
So I don't think the new
in the example you give has any significance and could just as well have been left out.
Source: stackoverflow.com
Related Query
- Using new with Scala final case class
- How to update a mongo record using Rogue with MongoCaseClassField when case class contains a scala Enumeration
- How to create new instance of Scala class with context bound via Java reflection using only zero argument constructor?
- Parse json array to a case class in scala using playframework with the fields in json not matching the fields in case class
- "Forward reference extends over definition value exp" when using an abstract class with case classes of a Scala tutorial
- No TypeTag available for a case class using scala 3 with spark 3
- Using a case class with map and nested case class with gremlin scala
- Using Spark converting nested json with optional fields to Scala case class not working
- Compare a list values with case class using Scala and Spark
- How to join multiple RDD having Object using scala with case class
- (Un)marshall JSON with named root for Ember Data using Scala case class on Spray
- Class broken error with Joda Time using Scala
- Scala case class extending Product with Serializable
- Scala - No TypeTag Available Exception when using case class to try to get TypeTag?
- Read CSV in Scala into case class instances with error handling
- Using Jackson to (De)-serialize a Scala Case Class
- Scala copy case class with generic type
- Scala wont pattern match with java.lang.String and Case Class
- Derived Scala case class with same member variables as base
- Scala spark: how to use dataset for a case class with the schema has snake_case?
- Replacing case class inheritance with extractors preserving exhaustiveness checks in Scala
- Problem with bounded type parameterised case class and default args in Scala
- How do I add a no-arg constructor to a Scala case class with a macro annotation?
- How to check which parameters of case class have default value using scala reflection 2.10
- Scala Dynamic Parse Json using case class No Manifest available for T
- Scala case class with function parameters
- compare case class fields with sub fields of another case class in scala
- dynamical creating a new instance of a case class in scala
- @JsonIgnore serialising Scala case class property using Jackon and Json4s
- Modeling with Scala case class
More Query from same tag
- Akka context become preserving state order
- Scala implicit TypeTag not propagating correctly (implicit type is Nothing)
- Spark: remove all duplicated lines
- SLICK 3.0 and postgres SQL: Apply `exists` on the result of SimpleFunction returning a list
- How to remove every element in an array after a conditional?
- How to do higher order function transform with sub query and a map lookup?
- What does it mean when object extends class with no implementation
- Overloaded method value json with alternatives: (jsonRDD: org.apache.spark.rdd.RDD[String]) using Spark in IntelliJ
- Read CSV with last column as array of values (and the values are inside parenthesis and separated by comma) in Spark
- Pros and cons of using sbt vs maven in Scala project
- On Spark 1.6.0, get org.apache.spark.SparkException related with spark.driver.maxResultSize
- TypedPipe can't coerce strings to DateTime even when given implicit function
- Scala - Futures not starting
- Problems sampling Double-Ranges in Scala
- spark shell (spark 2.4, scala 2.11.12 ) can not recognize imported class
- How to wrap Scala for comprehension to resolve Future around Play's Form folding?
- Is there a way to include a Condition to adquire method in java (or scala) Semaphore object?
- Spray routing works for single slash but nothing else
- Loading parquet files in hive table returns all NULL
- Finding uncommon elements in Seq of Tuples
- How do I give global access to an object in Scala without making it a singleton or passing it to everything?
- How does the col function know which DataFrame we are referring to?
- How to format strings in Scala?
- Stress test Akka Actor Mailboxes
- How to create spark dataframe from list
- Transfer Scala case class to JsValue in rdd.map func but Task not serializable
- What to import to make < work for jodatime in Scala
- Scala read file and split and modify each line
- Why does the Scala compiler say that copy is not a member of my case class?
- why doesn't scala infer type Any or AnyVal from Int and Double?