0

I've been trying to add data to a mutable list in scala.

I can add basic datatypes fine, thanks to Alvin Alexanders excellent blog post using a ListBuffer class, but when it comes to objects, Im not really sure how to proceed so please dont mark as duplicate just yet.

import scala.collection.mutable.ListBuffer

var fruits = new ListBuffer[String]()
fruits += "Apple"
fruits += "Banana"
fruits += "Orange"

So, I try to map this like so:

import scala.collection.mutable.ListBuffer
var fruits = new ListBuffer[MyClass]()
var d1=new MyClass("data1","data2","data3")

fruits += d1

This gives the following error:

type mismatch
found: MyClass
required: MyClass

Can anyone help me here?

1
  • This works for me in the REPL, can you add a more detailed description? Commented Aug 13, 2014 at 12:24

1 Answer 1

2

Unable to reproduce:

Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :paste
// Entering paste mode (ctrl-D to finish)

import scala.collection.mutable.ListBuffer
var fruits = new ListBuffer[MyClass]()
class MyClass(a:String, b:String, c:String)
var d1=new MyClass("data1","data2","data3")
fruits += d1

// Exiting paste mode, now interpreting.

import scala.collection.mutable.ListBuffer
fruits: scala.collection.mutable.ListBuffer[MyClass] = ListBuffer(MyClass@291c57ba)
defined class MyClass
d1: MyClass = MyClass@291c57ba
res0: scala.collection.mutable.ListBuffer[MyClass] = ListBuffer(MyClass@291c57ba)

EDIT:
You might have re-defined MyClass and it led to "type mismatch" error. Maybe something like this:

scala> :paste
// Entering paste mode (ctrl-D to finish)

import scala.collection.mutable.ListBuffer
class MyClass(a:String, b:String, c:String)
var fruits = new ListBuffer[MyClass]()

// Exiting paste mode, now interpreting.

import scala.collection.mutable.ListBuffer
defined class MyClass
fruits: scala.collection.mutable.ListBuffer[MyClass] = ListBuffer()

scala> :paste
// Entering paste mode (ctrl-D to finish)

class MyClass(a:String, b:String, c:String)
var d1=new MyClass("data1","data2","data3")
fruits += d1

// Exiting paste mode, now interpreting.

<console>:14: error: type mismatch;
 found   : MyClass(in object $iw)(in object $iw)(in object $iw)(in object $iw)
 required: MyClass(in object $iw)(in object $iw)(in object $iw)(in object $iw)
              fruits += d1
                        ^
Sign up to request clarification or add additional context in comments.

5 Comments

Here's the thing, Ive been trying each command one by one in scala terminal. Now, I tried the paste method and it works. Whats happening here?
I would guess you had an old instance of MyClass, then you redefined MyClass, created ListBuffer with new MyClass and tried pushing there an old instance of MyClass.
I redefined MyClass each time. Doesnt that override the old one?
Yes it does override class definition, but not the already created instances of the old MyClass, answer updated.
Thanks for the :paste didn't know it was there to be used in REPL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.