What specific data type is created when I use the Seq [T] constructor?
Scala allows me to instantiate an object that implements a property Seq
directly using this syntax:
val numbers: Seq[Int] = Seq(1, 2, 3)
val names: Seq[String] = Seq("Alice", "Bob", "Charles")
- Since it
Seq
is a trait and not a specific implementation, what datatype is underlyingnumbers
andnames
? - What's the best way I could figure this out for myself?
- Is it idiomatic to create an object object directly?
Thank!
+3
source to share
2 answers
List
is the default implementation. You can check this easily in the Scala REPL:
scala> val numbers: Seq[Int] = Seq(1, 2, 3)
numbers: Seq[Int] = List(1, 2, 3)
scala> val names: Seq[String] = Seq("Alice", "Bob", "Charles")
names: Seq[String] = List(Alice, Bob, Charles)
How much better (creation List
or Seq
) I think about preference. Both options are available and as far as I know the compiler doesn't complain when choosing one of them. I personally always use Seq
.
+3
source to share