Why does scala convert Seq to list?

> val a:Seq[Integer] = Seq(3,4) a: Seq[Integer] = List(3, 4) 

If Seq is just a sign, why does the / REPL compiler accept it and does it behave as it does for many other signs or even abstract classes?

+7
scala
source share
2 answers

It does not convert anything.

Seq is a sign, you cannot create it, but only mix it with some class.

Since the apply method of the Seq companion object must return some specific instance of the class (which mixes in the Seq line), it returns a List , which seems reasonable by default.

One situation in which this may be useful is when you need an instance of Seq , but it does not care about the implementation and does not have time to look at the type hierarchy to find a suitable class that implements Seq . Seq(3,4) guaranteed to give you what meets the Seq contract.

+8
source share

The default implementation for Seq is the list specified here in scaladoc:

Seq Object

What says

This object provides a set of operations for creating Seq values. The current default implementation of Seq is List.

When you call Seq(3,4) you really call Seq.apply(3,4) , which builds a sequence of two elements under the List under.

+6
source share

All Articles