How to declare empty list and then add string in scala? -
i have code this:
val dm = list[string]() val dk = list[map[string,object]]() ..... dm.add("text") dk.add(map("1" -> "ok"))
but throws runtime java.lang.unsupportedoperationexception.
i need declare empty list or empty maps , later in code need fill them.
scala lists immutable default. cannot "add" element, can form new list appending new element in front. since new list, need reassign reference (so can't use val).
var dm = list[string]() var dk = list[map[string,anyref]]() ..... dm = "text" :: dm dk = map(1 -> "ok") :: dk
the operator ::
creates new list. can use shorter syntax:
dm ::= "text" dk ::= map(1 -> "ok")
nb: in scala don't use type object
any
, anyref
or anyval
.
Comments
Post a Comment