Using context bounds in scala you can do stuff like
trait HasBuild[T] {
def build(buildable: T): Something
}
object Builders {
implict object IntBuilder extends HasBuild[Int] {
override def build(i: Int) = ??? // Construct a Something however appropriate
}
}
import Builders._
def foo[T: HasBuild](input: T): Something = implicitly[HasBuild[T]].build(1)
val somethingFormInt = foo(1)
Or simply
val somethingFromInt = implicitly[HasBuild[Int]].build(1)
How could I express the type of a Seq
of any elements that have an appropriate implicit HasBuild
object in scope? Is this possible without too much magic and external libraries?
Seq[WhatTypeGoesHere]
- I should be able to find the appropriate HasBuild
for each element
This obviously doesn't compile:
val buildables: Seq[_: HasBuild] = ???