Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>tl;dr</h2> <ul> <li><code>class C</code> defines a class, just as in Java or C++.</li> <li><code>object O</code> creates a <a href="http://en.wikipedia.org/wiki/Singleton_pattern" rel="noreferrer">singleton</a> object <code>O</code> as instance of some anonymous class; it can be used to hold static members that are not associated with instances of some class.</li> <li><code>object O extends T</code> makes the object <code>O</code> an instance of <code>trait T</code>; you can then pass <code>O</code> anywhere, a <code>T</code> is expected.</li> <li>if there is a <code>class C</code>, then <code>object C</code> is the <a href="https://docs.scala-lang.org/tour/singleton-objects.html#companions" rel="noreferrer">companion object</a> of class <code>C</code>; note that the companion object is <strong>not</strong> automatically an instance of <code>C</code>.</li> </ul> <p>Also see Scala documentation for <a href="https://docs.scala-lang.org/tour/singleton-objects.html" rel="noreferrer">object</a> and <a href="https://docs.scala-lang.org/tour/classes.html" rel="noreferrer">class</a>.</p> <h3>Usage as host of static members</h3> <p>Most often, you need an <code>object</code> to hold methods and values/variables that shall be available without having to first instantiate an instance of some class. This is use is closely related to <code>static</code> members in Java.</p> <pre><code>object A { def twice(i: Int): Int = 2*i } </code></pre> <p>You can then call above method using <code>A.twice(2)</code>.</p> <p>If <code>twice</code> were a member of some class <code>A</code>, then you would need to make an instance first:</p> <pre><code>class A() { def twice(i: Int): Int = 2 * i } val a = new A() a.twice(2) </code></pre> <p>You can see how this is redundant, as <code>twice</code> does not require any instance-specific data.</p> <h3>Usage as a special named instance</h3> <p>You can also use the <code>object</code> itself as some special instance of a class or trait. When you do this, your object needs to extend some <code>trait</code> in order to become an instance of a subclass of it.</p> <p>Consider the following code:</p> <pre><code>object A extends B with C { ... } </code></pre> <p>This declaration first declares an anonymous (inaccessible) class that extends both <code>B</code> and <code>C</code>, and instantiates a single instance of this class named <code>A</code>.</p> <p>This means <code>A</code> can be passed to functions expecting objects of type <code>B</code> or <code>C</code>, or <code>B with C</code>.</p> <h3>Additional Features of <code>object</code></h3> <p>There also exist some special features of objects in Scala. I recommend to read the <a href="https://docs.scala-lang.org/tour/singleton-objects.html" rel="noreferrer">official documentation</a>.</p> <ul> <li><code>def apply(...)</code> enables the usual method name-less syntax of <code>A(...)</code></li> <li><code>def unapply(...)</code> allows to create custom pattern matching <a href="http://docs.scala-lang.org/tour/extractor-objects.html" rel="noreferrer">extractors</a></li> <li>if accompanying a class of the same name, the object assumes a special role when resolving <a href="http://docs.scala-lang.org/tour/implicit-parameters.html" rel="noreferrer">implicit parameters</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload