Is it possible to set a variable which has scope in a method from it's child class in scala?
Why I need is explained in following code snippet:
abstract class SuperTask {
def doWork():Unit = {
var taskData:Option[TaskData] = None
try {
_actualWork()
}
catch {
// catch exception and do reporting using taskData variable
}
}
protected def _actualWork(): Unit
}
class FunTask extends SuperTask {
override def _actualWork(): Unit = {
//This will have data for taskData
//PROBLEM is how to set taskData of parent class from here so that if this throws exception, I can do reporting from catch in doWork method.
}
}
I want to set value of taskData
variable, so that if _actualWork
fails, I can have proper reporting, retries, etc which can be common for all child classes of SuperTask
class.
I can not define taskData
variable at SuperTask
class level as it then will be shared and fail in case of concurrency.