tmnd91
tmnd91

Reputation: 449

scala variable number of generic types in class

I'm working with scala and scalaStorm for a project, i'm currently using velvia's scalastorm library from github (https://github.com/velvia/ScalaStorm), and i'm trying to enrich it. I want to add type safety to storm tuple's that are by default all java Object. In storm there are entities called bolts that take tuple as input and output other tuples. I want to do something like this:

class StormBolt[T*][K*]{
}

So I can write directly:

class MyBolt[Int, Date, String][Int, String]{
}

I didn't find anything that let's me do this in some way. I appreciate any tip in implementing such feature! Adding type safety to the library wouldn't be a shame! Thank you

Upvotes: 6

Views: 1168

Answers (1)

Eugene Zhulenev
Eugene Zhulenev

Reputation: 9734

You can do it with simple generic types or use HList from shapeless(https://github.com/milessabin/shapeless/wiki/Feature-overview:-shapeless-2.0.0#heterogenous-lists)

trait StormBolt[T, K] {
}

trait MyBolt extends StormBolt[(Int, Date, String), (Int, String)]

or with shapeless

trait StormBolt[T <: HList, K <: HList] {
}

trait MyBolt extends StormBolt[Int :: Date :: String :: HNil, Int :: String :: HNil] {
}

with shapeless you can get lot's of cool features, you can take a look at feature overview, maybe you'll find some of them useful

Upvotes: 5

Related Questions