Scala Generic Function

[Solved] Scala Generic Function | Scala - Code Explorer | yomemimo.com
Question : scala generic function

Answered by : filaseta-angelo

def listOfDuplicates[A](x: A, length: Int): List[A] = { if (length < 1) Nil else x :: listOfDuplicates(x, length - 1)
}
println(listOfDuplicates[Int](3, 4)) // List(3, 3, 3, 3)
println(listOfDuplicates("La", 8)) // List(La, La, La, La, La, La, La, La)

Source : https://docs.scala-lang.org/tour/polymorphic-methods.html#:~:text=Methods%20in%20Scala%20can%20be,parameters%20are%20enclosed%20in%20parentheses.&text=The%20method%20listOfDuplicates%20takes%20a,value%20parameters%20x%20and%20length%20. | Last Update : Mon, 15 Mar 21

Answers related to scala generic function

Code Explorer Popular Question For Scala