Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"new" keyword in Scala

I have a very simple question - when should we apply the new keyword when creating objects in Scala? Is it when we try to instantiate Java objects only?

like image 205
Bober02 Avatar asked Mar 15 '12 20:03

Bober02


People also ask

What is new keyword in Scala?

Use the new keyword when you want to refer to a class 's own constructor: class Foo { } val f = new Foo. Omit new if you are referring to the companion object's apply method: class Foo { } object Foo { def apply() = new Foo } // Both of these are legal val f = Foo() val f2 = new Foo.

What is the keyword new?

New keyword in JavaScript is used to create an instance of an object that has a constructor function. On calling the constructor function with 'new' operator, the following actions are taken: A new empty object is created.

How do I create a new object in Scala?

In Scala, an object of a class is created using the new keyword. The syntax of creating object in Scala is: Syntax: var obj = new Dog();

Why the new keyword is used for?

The new keyword in JavaScript: The new keyword is used to create an instance of a user-defined object type and a constructor function. It is used to constructs and returns an object of the constructor function.


2 Answers

Use the new keyword when you want to refer to a class's own constructor:

class Foo { }  val f = new Foo 

Omit new if you are referring to the companion object's apply method:

class Foo { } object Foo {     def apply() = new Foo }  // Both of these are legal val f = Foo() val f2 = new Foo 

If you've made a case class:

case class Foo() 

Scala secretly creates a companion object for you, turning it into this:

class Foo { } object Foo {     def apply() = new Foo } 

So you can do

f = Foo() 

Lastly, keep in mind that there's no rule that says that the companion apply method has to be a proxy for the constructor:

class Foo { } object Foo {     def apply() = 7 }  // These do different things > println(new Foo) test@5c79cc94 > println(Foo()) 7 

And, since you mentioned Java classes: yes -- Java classes rarely have companion objects with an apply method, so you must use new and the actual class's constructor.

like image 164
Owen Avatar answered Sep 20 '22 18:09

Owen


Is it when we try to instantiate java objects only?

Not at all. There is two general cases when you ommit new in scala. With singleton objects (that are oftenly used to store static functions and as a kind of factory similar to what you may seen in java):

scala> object LonelyGuy { def mood = "sad" } defined module LonelyGuy  scala> LonelyGuy res0: LonelyGuy.type = LonelyGuy$@3449a8  scala> LonelyGuy.mood res4: java.lang.String = sad 

With a case classes (actually, underneath there are class + object = companion pattern, e.g. having class and object with the same name):

scala> case class Foo(bar: String)  defined class Foo   scala> Foo("baz") res2: Foo = Foo(baz) 

So when you work with a simple classes, rules are the same as with Java.

scala> class Foo(val bar: String)  defined class Foo  scala> new Foo("baz") res0: Foo = Foo@2ad6a0  // will be a error  scala> Foo("baz") <console>:8: error: not found: value Foo        Foo("baz") 

Bonus, there is a anonymous classes in scala, which can be constructed like this:

scala> new { val bar = "baz" } res2: java.lang.Object{val bar: java.lang.String} = $anon$1@10ee5b8  scala> res2.bar res3: java.lang.String = baz 
like image 26
om-nom-nom Avatar answered Sep 19 '22 18:09

om-nom-nom