Is it possible to have type parameters (generics) on classes which are not collections in scala? -
as questions suggests. ever make sense think generics on non-collections? have been trying think through. collection 'wrapper' of other objects, there has 'wrapper' direct generic at?
thanks
yep. minimal example, here class wraps of type t, t generic:
scala> class wrapper[t](val x: t) defined class wrapper if give int, it's wrapper[int]:
scala> new wrapper(5) res0: wrapper[int] = wrapper@578ef2b6 if give string, it's wrapper[string]:
scala> new wrapper("this") res1: wrapper[string] = wrapper@3b16bf07 this directly analogous collection of items of type t:
scala> vector(1,2,3) res2: scala.collection.immutable.vector[int] = vector(1, 2, 3)
Comments
Post a Comment