1

I am trying to return ComputingMethod as a (preferably immutable) List. I think I want to declare it as an empty list in a separate constructor but I'm not exactly sure how to begin or where to go thereafter. A simplified version of the code slice I have is:

class MyComputingMethod extends ComputingMethod{
   override def execute(o: Vector[Object]): ComputingMethod = {
      val wc = List[Object]() 
      for(i<-o.indices)
         wc::(o(i))                          //put vector objects in vector
      wc
   }
}

ComputingMethod is a trait that defines the execute method. I've declared Object in another class. So I want to return a ComputingMethod object but I need ComputingMethod to be a List. I'm not even sure if List should be declared as generic or as a list of Object (or as a Vector of Objects).

Things I've tried:

object MyComputingMethod {
   val mcm = List[Object]()                      //declaring outside class
   type ComputingMethod = List[Object]           //also tried within class
   val mc = new ComputingMethod().mcm            
   /*the above doesn't allow me to use any of the List operations, 
   although mc is of type VirtualMachine, 
   but the 'mcm' list doesn't seem connected */
}

class MyComputingMethod extends ComputingMethod{
   val mcm = List()                              //declaring outside def
   def mCm : MyComputingMethod = List()          //def rather than val, shot in the dark
   override def execute(o: Vector[Object): ComputingMethod = {
      val wc = List[Object]() 
      for(i<-o.indices)
         mCm.wc::(o(i))              //put vector objects in vector
      mCm.wc                        //attempt to return ComputingMethod List
   }
}

So I think what I I need to do is create a generic constructor (and a private?) for MyComputingMethod that within it initializes an empty list that I can use as a MyComputingMethod object, But I am unsure how to do this. Any suggestions are much appreciated.

jeffkozlow
  • 11
  • 2
  • What do you mean with "*I need `ComputingMethod` to be a `List`*" ? – Peter Neyens Oct 25 '15 at 09:12
  • can you replace `//method here` comments with something meaningful? I have no idea what you want to acomplish and why... – Łukasz Oct 25 '15 at 15:16
  • I need to create a ComputingMethod object that can behave like a stack to hold memory of objects that will be pushed and popped off the stack but I'm not sure how. Every time I try a variation of above I get a "return type of List[Object] does not agree with expected type ComputingMethod" message. – jeffkozlow Oct 25 '15 at 16:20
  • You should definitely put more info. Paste the `trait`, if possible. Also, Object is the java.lang.Object ? If not, please rename it, it causes a lot of confusion. – Onilton Maciel Oct 25 '15 at 20:06
  • You should state clear what you want. Do you *really* want to ComputingMethod be a extension of a List[ ]? Or do you just want to convert from ComputingMethod to a List []? Maybe forget about code and talk about what you want accomplish. – Onilton Maciel Oct 25 '15 at 20:09
  • In addition to @OniltonMaciel comments, if you just want to use some methods from the List interface, it might be best if ComputingMethod contains a list and then exposes those methods. See for example http://stackoverflow.com/questions/24301098/simple-example-of-extending-a-scala-collection – Marco Oct 26 '15 at 16:46

0 Answers0