1

I have a library, already provided, which has two Scala classes:

class EvaluationFunction (data:DataHolder) {
    def calculate(params:ParamHolder) = {...}
}

And

class Regression {
    def train(data:DataHolder) = {
        ...
        val costFunction = new EvaluationFunction(data)

        //find params to minimize costFunction.calculate(params)
        ....
    }
}

I want to make my own regression class, which behaves exactly like the original regression, but uses a different evaluation function. However, the only way I can think of to do that would involve extending Regression and overwriting the whole train method. But it contains pretty much all the logic in the Regression class - I may as well write it from scratch!

Is there a better way? For example, somehow tell my extended regression class to invoke another class every time the original invokes EvaluationFunction?

Todor Markov
  • 215
  • 1
  • 2
  • 7

1 Answers1

3

The code you show would not compile: the data does not have a datatype.

That aside - the structure of the Regression class is strange. It should have the EvaluationFunction as overridable. As it stands it is built into the train method. That is why you need to override (/rewrite) the entire class. It is a problem with the class structure.

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • I though the data type isn't really relevant, and skipped it. Anyway, the problem is, I don't have any control over the Regression class. – Todor Markov Dec 04 '17 at 09:26
  • The problem remains the same - the `Regression` class is mal-constructed. Go have a little chat with the library maintainers. Or .. rewrite the whole thing. – WestCoastProjects Dec 04 '17 at 09:37