Scala repeat array

I am new to scala . I am trying to write a function that "repeats" Array (Scala 2.9.0):

 def repeat[V](original: Array[V],times:Int):Array[V]= { if (times==0) Array[V]() else Array.concat(original,repeat(original,times-1) } 

But I can not compile this (receive a manifest error message) ...

+6
source share
2 answers

You need to ask the compiler to provide a class manifest for V :

 def repeat[V : Manifest](original: Array[V], times: Int): Array[V] = ... 

The answer to the question: why is this necessary, you can find here:

Why is ClassManifest needed with an array but not a list?

I'm not sure where you want to use it, but I can recommend you use List or another suitable collection instead of Array .

+6
source

BTW, an alternative way of repeating an array, would be to "populate" Seq with array references, and then smooth out the following:

 def repeat[V: Manifest](original: Array[V], times: Int) : Array[V] = Seq.fill(times)(original).flatten.toArray; 
+5
source

Source: https://habr.com/ru/post/890926/


All Articles