score:2
what you're trying to do is very easy to achieve using a variant of a pattern called magnet. first let's define some types:
sealed trait in
case class in1() extends in
case class in2() extends in
sealed trait out //mystery2
case class out1() extends out
case class out2() extends out
sealed trait name //mystery1
case class name1() extends name
case class name2() extends name
sealed trait row //mystery3
case class row1() extends row
case class row2() extends row
then we can prepare our magnet
typeclass:
sealed trait magnet[n <: name, i <: int, o <: out] {
type result <: row
def apply(name: n, in: i, out: o): result
}
inside of companion object of magnet
you can add entries to "lookup" as implicit values:
import scala.languagefeature.implicitconversions //you can also enable implicit conversions in build.sbt
object magnet {
implicit val function1 = new magnet[name1, in1, out2] {
override type result = row1
override def apply(name: name1, in: in1, out:out2): row1 = ???
}
implicit val function2 = new magnet[name2, in2, out1] {
override type result = row2
override def apply(name: name2, in: in2, out: out1): row2 = ???
}
}
finally, we can prepare invoketherightfunction
function, which will require implicitly magnet
:
def invoketherightfunction[n <: name, i <: int, o <: out](name: n, in: i, out: o)(implicit magnet: magnet[n,i,o]): magnet.result = magnet(name, in, out)
let's test it out:
val r1: row1 = invoketherightfunction(name1(), in1(), out2())
val r2: row2 = invoketherightfunction(name2(), in2(), out1())
Source: stackoverflow.com
Related Query
- Is it possible to build type-driven function lookup tables in Scala?
- Scala underscore - ERROR: missing parameter type for expanded function
- scala anonymous function missing parameter type error
- Scala unexpectedly not being able to ascertain type for expanded function
- Is it possible to write down a dependent function type in Scala?
- Why does this Scala function compile when the argument does not conform to the type constraint?
- Is it possible in Scala to force the caller to specify a type parameter for a polymorphic method?
- Is it possible in Scala to specify a constraint on a generic type τ such that τ <: σ ∧ τ ≠ σ?
- How to use the function type in scala within defined in type meaningfully?
- Scala Generic Function Values (Anonymous Function) - Missing Parameter Type (Error)
- Missing parameter type for expanded function in Scala ArrayBuffer
- Scala reduceByKey function - use any type that has + method
- Scala missing parameter type for expanded function
- How to turn off Scala auto-completion of function with Unit return type in IntelliJ IDEA?
- Scala Puzzle: enforcing that two function arguments are of the same type AND both are a subtype of a given class
- How to build a Map of lists of map from type safe config in scala
- Function type with receiver in Scala
- scala generic function return type
- Scala type-level identity function in a type parameter
- Type of a function with Implicit parameters in Scala
- Scala String* type (in function args)
- Why is my Scala function returning type Unit and not whatever is the last line?
- Scala ActionListener / anonymous function type mismatch
- Scala Partial Function Type Definition
- Is it possible to print definition of a function in Scala
- Scala Type Inference Issues with Parametric Function
- How do I know the type of a scala function
- Implement product type in Scala with generic update function working on its parts
- scala 2.9: plans for type inference of function parameters with default arguments?
- Scala type check complaining with lambda, not with explicit Function
More Query from same tag
- Is there a conversion/migration guide for Scala v2.9 to current v2.12.x?
- How do you write a CSV back to Azure Blob Storage using Databricks?
- How to do a `getOrWaitUntilNonEmpty` as a single liner?
- withFilter instead of filter
- Define function signature to prohibit some data types using type constructors
- How to make akka-http to forward a GET request to a POST request and change attributes?
- Add optional property with json transformers in playframework 2.4
- Does Java support anonymous functions in a similar way to scala?
- How to trim an option string in a Slick column mapping when using Play Scala
- Does Scala have an operator similar to Haskell's `$`?
- Is possible a ActorRef change without I receive a DeadLetter message?
- Spark UDF thread safety
- Is there an equivalent to SuppressWarnings in Scala?
- aggregating with a condition in groupby spark dataframe
- How to specify file name for a download done via POST in akka http
- Slick 2.0.0-M3 table definition - clarification on the tag attribute
- Split string by commas then Store in array val in Play! + Scala
- Convert string to a parameterized type in scala
- Scala somewhat Complex class structure to Json Object
- get "missing parameter type for expanded function" error when declaring a partial function
- Spark-Cassandra Maven project with java source making scala-lib calls
- Scala map on iterator does not produce side effects
- Flink: Broadcast a map of values created using keyBy operator to another stream, cold start problem
- How to map elements of array to every record in a Spark Dataframe
- Simple matrix calculation in Scala
- Scala MongoDB groupby multiple fields
- Apply on companion object to aliased type
- How to connect graphenedb / neo4j to play app running on heroku?
- Combining a list of maps in scala
- Group List elements with a distance less than x