I wrote the following code but I'm getting errors because it isn't the right way to implement it in Scala. I'm new to Scala, so if you can tell me how exactly I can define nextObj so that it can later get initialized it will be great.
trait myInterface[A]
{
}
class MyClass[A] extends myInterface[A]{
var myVal:A=_
var nextObj:MyClass[Any]=_
def mapNextObj[B](map_func:A=>B):myInterface[B]={
nextObj=new MyClass[B]()
nextObj.myVal=map_func(myVal)
nextObj
}
def cleanAllLinkedObjects():Unit={
nextObj.cleanAllLinkedObjects()
myVal=_
}
I want to be able to initialize the nextObj in the mapNextObj func. In addition, I need to keep nextObj as a data member because I want to cleanAll objects in some point while I have in my main only the first Obj.
I used just an example, but I'm trying to get the idea of how I can do something like that in Scala.
The issues I'm facing:
I can't initialize nextObj in mapNextObj as MyClass[B]. Getting : required MyClass[Any] Found : MyClass[B]
Same error as in 1 but regarding return code (required MyClass[B] found MyClass[Any]