Is this possible to get:
object test[A](l: Int): List[A] = {
...
}
You know what i mean. Is that possible?
Is this possible to get:
object test[A](l: Int): List[A] = {
...
}
You know what i mean. Is that possible?
Traits or Objects cannot have constructor parameters, and objects cannot have type parameters.
An object is effectively a singleton instance of a class. Therefore you can't have a generic definition of it, because it has only one name.
Your best option is to define a method within an object which returns the list you want.
Maybe you mean something like
object MyListMaker {
def test[A](a0: A, n: Int): List[A] = (1 to n).map(_ => a0).toList
}
scala> MyListMaker.test("Fish",7)
res0: List[java.lang.String] = List(Fish, Fish, Fish, Fish, Fish, Fish, Fish)
Only one copy of an object exists; if you want to create a method that does something generic, add the type parameter to the method (not the object).