Initialize a list from a function return in Scala -
how declare list var initialized returning list of funtion?
i want list[any]
, let's call newlist
, assigned values returned function, makelist()
, returns list[any]
.
like:
var newlist = makelist(arg1, arg2)
which gives error: "expression of type doesn't conform expected type list[any]"
i want index first element of newlist like: newlist(0)
i have tried val newlist : list[any] = makelist(arg1, arg2)
still error message @ newlist(0)
i've confirmed makelist
returns list[any].
the last error coming having newlist(0)
last line in function, so, last question be: return type of function need returns element of list[any], any?
the way assigning correct.
example,
scala> def makelist = list("scala", "clj", 1, 100.5) makelist: list[any] scala> val newlist = makelist newlist: list[any] = list(scala, clj, 1, 100.5) scala> newlist = list("i want change reference of list") <console>:12: error: reassignment val newlist = list("i want change reference of list") ^
in above example newlist val
meaning can't mutate later.
using var
, can change reference
scala> def makelist() : list[any] = list("scala", "clj", 1, 100.5) makelist: ()list[any] scala> var newlist = makelist newlist: list[any] = list(scala, clj, 1, 100.5) scala> newlist = list("i changed reference of list") newlist: list[any] = list(i changed reference of list)
Comments
Post a Comment