I am a Java programmer who performs baby steps in Scala programming.
I defined a function similar (which might be idiomatically incorrect, I would not know ...):
def someGenericAlgorithm(param1: String, param1: String, param3: String) = { val triedResult1 = someFunction(param1) triedResult1 match { case Success(result1) => val triedResult2 = someOtherFunction(param2) triedResult2 match { case Success(result2) => val triedPolymorphicResult = someToBeReplacedAccordingToSpecificLogicFunction(result1, result2, param3) triedPolymorphicResult match { case Success(polymorphicResult) => doSomethingGeneric(polymorphicResult) .... case Failure(exception) => .... } case Failure(exception) => .... } case Failure(exception) => .... } }
Thus, the function receives 3 parameters, processes the first two in the sequence, takes the processing results and passes it along with someToBeReplacedAccordingToSpecificLogicFunction along the side of the third parameter.
Now I want to write a similar function in which the first two function calls are present, the error logic is the same, and the only difference is that someToBeReplacedAccordingToSpecificLogicFunction now aReplacementLogicFunction .
If two functions had the same signature, I think that would not be a problem, but they do not, aReplacementLogicFunction has different parameters, or rather two more parameters. Then you can say that I can simply pass Option parameters, where these two Option are None in the first case, and so I would compare my two functions with the same signature, like this:
def someToBeReplacedAccordingToSpecificLogicFunction(result1: Int, result2: Int, param3: String, unused1:Option[String] = None, unused2:Option[String] = None) def aReplacementLogicFunction(result1: Int, result2: Int, param3: String, used1:Option[String], used2:Option[String])
But there is a third thread where non-existent arguments of the first function are of a different type, for example MyClass, for example:
def anotherReplacementLogicFunction(result1: Int, result2: Int, param3: String, used1: MyClass, used2: MyClass)
The question is, how could I pass these functions along with some refactored universal function of the algorithm, which would receive the function as a parameter. Is there a way to define something like:
def refactoredOutCoreLogic(param1: String, param1: String, param3: String, polimorphicFunction: SignatureUnknown)
To answer your own question in Java :
The way I solved this in Java is to define a class hierarchy, akin to:
abstract class ResultKeeperRunnable implements Runnable { protected int param1, param2; void setParam1(...) {...} void setParam2(...) {...} } class SomeToBeReplacedAccordingToSpecificLogicFunctionClass extends ResultKeeperRunnable { String param3;
Finally, a complete main logical method would be something like this:
void refactoredOutCoreLogic(String param1, String param2, String param3, ResultKeeperRunnable polimorphicLogic) {
Will the Scala solution be the no more syntactically distinct clone of the Java solution presented above? There seems to be a more elegant solution.