apply() / update()

apply() / update()

讓 Compiler 做更多事

update( )

i.e scala.Array {class, object}

def update(i: Int, x: T): Unit

Update the element at given index .

Indices start a 0; xs.apply(0) is the first element of array xs.
Note the indexing syntax xs(i) = x is a shorthand for xs.update(i, x).

    the index
    the value to be written at index i

apply( )

trait Function0[+R] extends AnyRef

Function with 0 parameters .
In the following example the definition of currentSeconds is a shorthand for the anonymous class definition anonfun0:

object Main extends Application {
    val currentSeconds = () => System.currentTimeMillis() / 1000L
    val anonfun0 = new Function0[Long] {
        def apply(): Long = System.currentTimeMillis() / 1000L

    println(currentSeconds()) println(anonfun0())
known subclasses: Future, Reference, AbstractFunction0

Value Members
def apply(): R
attributes: abstract

val list = Array(1, 2, 3)
list: Array[Int] = Array(1, 2, 3) 
val list = Array.apply(1, 2, 3)
list: Array[Int] = Array(1, 2, 3)