Is Array[String] not a subclass of Seq[String] in Scala?
Asked Answered
S

2

10

I wrote a method that accepts objects of all subclasses of Seq[String]. Unfortunately it won't accept an object of the type Array[String]. Is Array[String] not a subclass of Seq[String]?

scala> def test[T <: Seq[String]](x: T) = {}
test: [T <: Seq[String]](x: T)Unit

scala> val data = "This is a test string"
data: java.lang.String = This is a test string

scala> test(data.split(" "))
<console>:10: error: inferred type arguments [Array[java.lang.String]] do not conform to method test's type parameter bounds [T <: Seq[String]]
              test(data.split(" "))
Sudan answered 17/7, 2012 at 12:59 Comment(0)
I
11

No, Array[String] translates to regular JVM arrays, like the ones you see in Java: String[].

The reason why you see all the operations on Array[String] that you see on other Scala Seq collections is that there is an implicit conversion from Array[T] to ArrayOps[T].

Do this:

def test[T <% Seq[String]](x: T) = {}

This is called a view bound. It means that T should either be a subtype of Seq[String] or there should exist an implicit conversion in scope which converts T into a Seq[String]. Behind the scenes, the compiler actually adds an implicit parameter to test, so this method becomes:

scala> def test[T <% Seq[String]](x: T) = {}
test: [T](x: T)(implicit evidence$1: T => Seq[String])Unit

This implicit evidence$1 is the function which now acts as the implicit conversion from T to Seq[String] within the body of the method.

Isom answered 17/7, 2012 at 13:4 Comment(2)
explain the work of the view bound a bit and this answer will explain all important things going on here.Ademption
Very enlightening. I am still new to Scala and view bounds are new to me. I will get into that. Thanks for your rapid reply.Rhetic
U
5

The sources (or the API docs) state, that Array is defined as

final class Array[T] extends Serializable with Cloneable

That is, it is not a subtype of Seq. However, the docs also mention an implicit conversion WrappedArray, where the latter is a subset of Seq.

Unduly answered 17/7, 2012 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.