Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the benefit of Scala (singleton) object vs. typical class level artifacts [closed]

I understand one of the main benefits from using object is, they are real objects instead of system wide functionality. But finally those objects are also system wide accessible.

Beside of being more "pure" what additional benefit does scala "objects" offer.

I bet there are a number but I can't really figure out which.

like image 968
OscarRyz Avatar asked Dec 04 '22 10:12

OscarRyz


1 Answers

  • objects are independent entities, e.g. they can be used as method arguments, as target for implicit conversions, and case objects in pattern matching...
  • objects can inherit from classes or traits
  • an object has its own type
  • objects can restrict the access of its members, even for the companion class:

.

object X {
  private[this] val z=1 
}

class X { 
  import X._ 
  //won't compile 
  println(z)
}
like image 142
Landei Avatar answered Jan 16 '23 13:01

Landei