Alex Rivera | Logout

Scala Arrays vs Vectors

Asked 2011-05-25T21:40:40.633
9

Scala newb... I'm confused

object myApp extends App {
  println("Echo" + (args mkString " "))
}

"args" is type Array[String], but in the scaladoc, Array has no such method. mkString is a method for Vector, but I don't see any inheritance link between the two. So why can we use the mkString method on args?

Edit
Report

1 Answer

17

I'm not a scala expert (far from it!) but I think the answer is implicit conversions (see scala.Predef) and WrappedArray.scala.

In particular, Predef has the following implicit conversion:

implicit def genericWrapArray [T] (xs: Array[T]): WrappedArray[T] 

And WrappedArray has a mkString method. When scala can't find a mkString method on Array, it looks for an implicit conversion to a type that does.

http://www.scala-lang.org/api/current/scala/Predef$.html

http://www.scala-lang.org/api/current/scala/collection/mutable/WrappedArray.html

answered 2011-05-25T21:47:54.983

Your Answer