collections - groovy list api creating new lists versus modifying current list -


just not fathom why groovy list api have classes of methods that

a) allow modification of current list

b) return new list,

e.g.

alist-['hello','world']  // modifying list alist.set(2,'modifying') //will modify alist itself, i.e. alist=['hello','world','modifying'  //create new list without modifying self alist.plus(1,'worlds')   //alist not modified, although statements creates new list: ['hello','worlds','modifying'] 

my question is: why wouldn't authors of groovy stick standard, i.e. either have immutable collections methods either return new list instances without modifying current collection instance, or allow methods such 'plus' modify current collection. seems little un-intuitive have 2 standards on this.

groovy wraps around java , allows call existing java methods.

so set java method manipulates underlying list. therefore in groovy, calls method , mutate list.

minus, plus , left-shift groovy additions, in case of lists, written not mutate original lists.

when comes maps however,left-shift mutate map, guess have remember.


Comments