score:1
Accepted answer
Would this suffice? (It compiles...)
/* Classes defined by the framework */
abstract class Node {
def getMessage(n: Node): Int
}
def importantAlgorithm(lstNodes1: List[Node], lstNodes2: List[Node]) {
lstNodes1.zip(lstNodes2).map {
case (n1, n2) =>
// I would like to get the proper message *BASED ON
// THE TYPE OF N1 and N2*
val message = n1.getMessage(n2)
}
}
/* Classes defined by framework users */
class ItemNode(val p: Int)
extends Node
{
def getMessage(n: Node): Int =
n match {
// Compute message based on this ItemNode member variables
// and n (instance of UserNode) member variables
case un: UserNode => 0
case in: ItemNode => 1
case xn: Node => -1
}
}
class UserNode
extends Node
{
def getMessage(n: Node): Int =
n match {
case on: OtherNode => 23
case xn: Node => -1
}
}
class OtherNode
extends Node
{
def getMessage(n: Node): Int =
n match {
case xn: Node => 514
}
}
// The user should be able to use the framework this way
importantAlgorithm(List(new UserNode(),
new ItemNode(236),
new OtherNode()),
List(new OtherNode(),
new ItemNode(542),
new UserNode()))
score:1
I think you need is something like
trait Node {
type AppropriateSender <: Node
def getMessage(n: AppropriateSender): Int
}
class UserNode extends Node {
type AppropriateSender = OtherNode
def getMessage(n: OtherNode) = ???
}
...
However, there are some problems caused by type erasure so that you cannot check the compatibility of your n1
and n2
(maybe type tags?), but at least you can implement your staff in a clean way now. Another issue is how you deal with the fact that some node types have more than 1 appropriate sender type (which might be solved by an implementation of raw union type).
Source: stackoverflow.com
Related Query
- Replacement for specialization of method parameters in subclasses
- Syntax sugar: _* for treating Seq as method parameters
- Method parameters validation in Scala, with for comprehension and monads
- Unapplied method to function conversion not working for parameters in overloaded functions
- Patterns for same method & same parameters but different parameter names in scala
- no type parameters for method flatMap
- Inferring type parameter of method from method argument fails in Scala?: "no type parameters for method"
- What are the rules for when trait/class type parameters take precedence vs method type parameters
- In Scala, how to use reflection to get the real type of varargs parameters for method
- Understanding Scala method overloading, optional/default parameters for an API client lib
- Build, using Shapeless, generic default instances for case classes with parameters defining a common createValue method
- difficulty when using contravariant type parameters for both a type parameter and return type of a method
- How to name parameters for a function in a method signature in scala?
- Getting "too many arguments for method apply" routes with multiple parameters
- Pattern for parsing method parameters
- Is there any replacement or alternative way for using scala.collection.TraversableOnce.mkString() method in java 7
- JsonIgnore for Scala method with default parameters does not work as expected
- What are some compelling use cases for dependent method types?
- Scala generic method - No ClassTag available for T
- What is the rule for parenthesis in Scala method invocation?
- How can Scala receive multiple parameters in a method definition?
- Scala replacement for Arrays.binarySearch?
- Check for None in Scala Option type isEmpty method
- What code is generated for an equals/hashCode method of a case class?
- How to provide default value for implicit parameters at class level
- Howto model named parameters in method invocations with Scala macros?
- What is the scala percent operator (%) and at method for strings do?
- Cats: Non tail recursive tailRecM method for Monads
- How to write copy() method for Simple Class in Scala
- scala: why does underscore (_) initialization work for fields but not method variables?
More Query from same tag
- Spark - How to convert map function output (Row,Row) tuple to one Dataframe
- Scala: reconciling type classes with dependency injection
- Gson in Scala: extra field `bitmap$0` in serialized Json output
- ColumnNotFound problem with Magic in play scala
- Controlling spawning of actors in Akka who consume noticeable amounts of memory
- Which Object is the consumer for Scala REPL methods?
- method calling using classes in scala
- Scaladoc swallowed @throws and @note tags
- What are the differences between mapcat in Clojure and flatMap in Scala in terms of what they operate on?
- Scala - Deleting spaces
- What is the best way to write a Scala object to Parquet?
- Define a function reference so that it retains its argument names
- What is the difference between these two method definitions?
- How to go from an functor over an effect to an effect over the functor?
- Use spark dataframe column value as an alias of another column
- Convert value string to Date, Scala Spark
- Scala: Code only run when debugging(#ifdef equivalent?)
- nondeterminism.njoin: maxQueued and prefetching
- Play framework create DEB and deploy it on Ubuntu 14.04
- write different grouped rdd values to one file
- Comparing type mapped values in Slick queries
- Adding module dependency information in sbt's build.sbt file
- Unable to access Scala's Akka libraries in JRuby
- Apache Spark Scala : How to maintain order of values while grouping rdd by key
- Parsing complex JSON with JSON Combinators in Play
- Override abstract type member in stackable trait pattern
- How to check if some type confirms to generic type bounds in Scala macros?
- How to Convert Integer to Varchar(8) using spark scala
- TableQuery to case class
- How to make Scala dependencies smaller?