Scala a variable number of common types in a class

I am working with scala and scalaStorm for a project, I am currently using the velvia scalastorm library from github ( https://github.com/velvia/ScalaStorm ) and I am trying to enrich it. I want to add a security type to a stormtuple, which by default is all java Object. There are objects in the storm called bolts that take a tuple as input and output other tuples. I want to do something like this:

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

Therefore, I can write directly:

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

I did not find anything that allowed me to do this in some way. I appreciate any advice in implementing such a feature! Adding type safety to the library would not be a shame! Thanks you

+4
source share
1 answer

You can do this with simple generic types or use the HList from formless ( 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 formless

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

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

with formless you can get a lot of interesting functions, you can take a look at the overview of functions, maybe you will find some of them useful

+4
source

Source: https://habr.com/ru/post/1568490/


All Articles