A related question is "Is it the same with imported Java classes?"
How can I workaround, if I need to fill an Array inside a function with copies of an instance passed as argument?
[Regarding identical copies, it was worth to me to check out the easy cloning library.

3 Answers
3

The fill method is defined with it's second parameter as "call-by-name". This means that the passed-in block is re-evaluated for every cell in the Array. See in the definition that the type of elem is => T, not simply T:

def fill[T: ClassManifest](n: Int)(elem: => T): Array[T]

So in your first version, the block new A is re-evaluated for each cell, meaning that each cell gets a fresh A object. In the second version, new A is called only once, and that object is placed into every cell.