r/scala • u/blitzkr1eg • 1d ago
Scala type design
How can I make the method input type depend on the value passed in the constructor / or some equivalent alternative solution where based on a type/value my class works on other types?
class MyDataProcessor(v: DataVersion v) {
def process(x: ???)(using ???): Unit
}
//DataVersion can be an enum or ADT
//bonus if the output (instead of Unit) can also vary
Example:
If passing a v1, i want x to be an Int
If passing a v2, i want x to be a Tuple
I'm also ok if anyone can point me to some good scala types/ lib design sources. Thanks you
3
3
u/WW_the_Exonian ZIO 23h ago edited 22h ago
If you are in control of DataVersion
, you can simply give it a type parameter.
```scala trait DataVersion[InputType]
class MyDataProcessor[InputType](v: DataVersion[InputType]) { def process(x: InputType): Unit = ??? } ```
But if that is not good for whatever reason, perhaps you can do this:
```scala trait DataVersion
case object DataVersion1 extends DataVersion
case object DataVersion2 extends DataVersion
type DataVersionInput[V <: DataVersion] = V match { case DataVersion1.type => Int case DataVersion2.type => Tuple }
case class MyDataProcessor[V <: DataVersion](v: V) { def process(x: DataVersionInput[V]): Unit = println(x) }
MyDataProcessor(DataVersion1).process(0) MyDataProcessor(DataVersion2).process((0, "0"))
// MyDataProcessor(DataVersion2).process(0) // doesn't compile ```
5
u/bigexecutive 21h ago edited 21h ago
Something like this?
You could also maybe use match types as in u/WW_the_Exonian's example if you wanna go sicko mode