deltanovember
deltanovember

Reputation: 44051

Why does my clone fail in Scala?

In the following I can clone the Hashtable but not the actual value

  val myHash = new HashMap[String, Int]
  myHash.put("A", 4)
  println(myHash("A").clone()) // fail
  myHash.clone() // works

I get the error cannot resolve symbol clone

Upvotes: 1

Views: 184

Answers (2)

thoredge
thoredge

Reputation: 12601

The HashMap is a Clonable while the Int is not.

I assume you expect to get an error since the int is in the map; however that is not a problem as clone is shallow. Clone will only create a new map and stuff all same keys and values into that.

Upvotes: 5

Adam Lear
Adam Lear

Reputation: 38768

You can't clone a number. Int doesn't have a clone() method.

Upvotes: 9

Related Questions