Greedy Coder Greedy Coder -4 years ago 71
Scala Question

Understanding update method in scala

I can understand the

apply
method in scala which basically is applying a function to a value. In many cases this is used as a form of constructor.

Let's assume that
list
is a map. An example would be
def apply(id : Int) = list(id)
, where it gets the given element from the list.

I also came across the example of the update method as

def update(id:Int,name:String) = list = list + (id -> name)
.

I can understand what this does i.e it updates/adds a key value pair in the list. What I cannot understand is the method contains two
=
symbols, one after the method definition and one which after list which defines what the method actually does
list + (id -> name)
.

What is the need for two
=
signs? How is one different from one another? I know that after the
=
sign the method body starts, but this is confusing.

Answer Source

You can view it as:

def update(id: Int, name: String) = {
  // perform update on 'list'
  list = list + (id -> name)
  // return empty (Unit) value
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download