I want to define a function like this
def showList(items: Seq[_ : Show]) = {...}
type parameter of Seq
is any type that has an context bound Show
Thus I could call showList(Seq(1, "abc"))
.
How should declare the method ?
def showList[A: Show](items: Seq[A]) = { ... }
or more explicitly (with the advantage of having the type class instance directly available)
def showList[A](items: Seq[A])(implicit ev: Show[A]) = { ... }
For the specific case, since scala collections are monomorphic, you'll need a Show[Any]
instance.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.