Tuesday, 15 September 2015

scala - Replacement for specialization of method parameters in subclasses -



scala - Replacement for specialization of method parameters in subclasses -

i create framework users able subclass base of operations class, node able produce message (here integer) based on current instance state, , properties of instance (the parameter n). users should able specialize method getmessage produce different messages based on class of current instance , the class of parameter n, shown in code below.

the function importantalgorithm uses messages generated these nodes compute final results.

// classes defined framework // abstract class node { def getmessage(n: node) : int } def importantalgorithm(lstnodes1: list[_ <: node], lstnodes2: list[_ <: node]) = { val results = lstnodes1.zip(lstnodes2).map({case (n1, n2) => // proper message *based on // type of n1 , n2* val message = n1.getmessage(n2) // work message //... //... }) //... } // classes defined framework users // class itemnode(val p: int) extends node { override def getmessage(n: usernode) = { // compute message based on itemnode fellow member variables // , n (instance of usernode) fellow member variables } override def getmessage(n: itemnode) = { // compute message based on itemnode fellow member variables // , n (instance of usernode) fellow member variables // algorithm different algorithm // used in previous method } } class usernode extends node { override def getmessage(n: othernode) = { // compute message. same thought above } } class othernode extends node { override def getmessage(n: usernode) = { // compute message. same thought above } } // user should able utilize framework way importantalgorithm(list(new usernode(), new itemnode(236), new othernode(), list(new othernode(), new itemnode(542), new usernode()))

of course, scala not allow specialize parameter of method in subclass , above code not compile. utilize isinstanceof[] or rtti have feeling not thinking , not designing framework properly. how replace mechanism described in code sample above simpler , cleaner solution ?

would suffice? (it compiles...)

/* classes defined framework */ abstract class node { def getmessage(n: node): int } def importantalgorithm(lstnodes1: list[node], lstnodes2: list[node]) { lstnodes1.zip(lstnodes2).map { case (n1, n2) => // proper message *based on // type of n1 , n2* val message = n1.getmessage(n2) } } /* classes defined framework users */ class itemnode(val p: int) extends node { def getmessage(n: node): int = n match { // compute message based on itemnode fellow member variables // , n (instance of usernode) fellow 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 } } // user should able utilize framework way importantalgorithm(list(new usernode(), new itemnode(236), new othernode()), list(new othernode(), new itemnode(542), new usernode()))

scala polymorphism

No comments:

Post a Comment