Scala abstract class instance

toto'

I am new to Scala. I have question about List class. This is an abstract and sealed class. It means cannot be instantiated, neither extended. So what is its usage? How can it be that something like following works?

val myList = List(1,2,3)

Would myList be a reference to the List object isntance? Also, if productElement is an abstract method how can that be implemented?

thanks.

Yuval Itzchakov

Regarding:

val myList = List(1,2,3)

When the compiler encounters an expression which creates a class instance without the new modifier, it looks up that classes companion object to look for a .apply method. In the case of list, it is defined as:

override def apply[A](xs: A*): List[A] = xs.toList

Thus, this compiles successfully. You can view this when asking the compiler to emit type information after the typer phase:

def main(args: Array[String]): Unit = {
  val l: List[Int] = scala.collection.immutable.List.apply[Int](1, 2, 3);
  ()
}

Would myList be a reference to the List object isntance?

The run-time type of myList would be either a cons (::) or the empty list (Nil).

if productElement is an abstract method how can that be implemented?

This is a compiler trick. productElement and productArity (and more) are both generated at compile time for any case class definition. For example, given the following case class:

case class Bar(i: Int)

The compiler generates:

// an incomplete view of the generated case class methods and fields
// omitted for brevity.
case class Bar extends AnyRef with Product with Serializable {
  <caseaccessor> <paramaccessor> private[this] val i: Int = _;
  <stable> <caseaccessor> <accessor> <paramaccessor> def i: Int = Bar.this.i;
  def <init>(i: Int): yuval.tests.Foo.Bar = {
    Bar.super.<init>();
    ()
  };
  <synthetic> def copy(i: Int = i): yuval.tests.Foo.Bar = new Bar(i);
  <synthetic> def copy$default$1: Int = Bar.this.i;
  override <synthetic> def productPrefix: String = "Bar";

  // this is the relevant part to your question
  <synthetic> def productArity: Int = 1;
  <synthetic> def productElement(x$1: Int): Any = x$1 match {
    case 0 => Bar.this.i
    case _ => throw new IndexOutOfBoundsException(x$1.toString())
  };

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Typescript: instance of an abstract class

Scala Return abstract class

Instance of abstract class with hidden constructor

Create an instance of an abstract class in Kotlin

Generic instance variable in abstract class

Scala abstract class function call

Trait vs Abstract Class in Scala

Is Traits in Scala an Interface or an Abstract Class?

Implement addition in abstract Scala class

Play Scala: class needs to be abstract

Private constructor in abstract class Scala?

Calling a method on an instance of a class that inherits an abstract class

determining if a Class object is an instance of an abstract class

Scala: Is it a class instance?

Typescript, type of child of abstract class, cannot create an instance of an abstract class

Implementing an abstract, generic Java class with abstract member class in Scala

Why can a abstract class be instance in Kotlin?

Cannot create an instance of an abstract class (Random)

Cannot create an instance of an abstract class Random kotlin

Generics: Create instance from abstract class

NHibernate cannot create instance of abstract class

Creating an instance of a subclass extending an abstract class (Java)

can we make instance of abstract class in java

How to resolve concrete non abstract class instance

Instance child class in abstract static method

Dynamically creating object instance of interface / abstract class

Adding an instance of subtype of abstract class in typescript

Change instance of abstract class that use generics

study case in C++. Abstract class and instance