Urlaub -> Scala (2)
Scala
Call-By-Reference und Call-By-Value sind (sollten) ja jedem Programmierer bekannt sein und die Anwendung sollte keine große Probleme bringen
Call-By-Name existiert in Scala -> Hier die Spec:
4.6.1 By-Name Parameters
Syntax:
ParamType ::= ‘=>’ Type
The type of a value parameter may be prefixed by =>, e.g. x: => T . The type of
such a parameter is then the parameterless method type => T . This indicates that
the corresponding argument is not evaluated at the point of function application,
but instead is evaluated at each use within the function. That is, the argument is
evaluated using call-by-name.
Kategorie Call-By-Reference und Call-By-Value sind (sollten) ja jedem Programmierer bekannt sein und die Anwendung sollte keine große Probleme bringen
Call-By-Name existiert in Scala -> Hier die Spec:
4.6.1 By-Name Parameters
Syntax:
ParamType ::= ‘=>’ Type
The type of a value parameter may be prefixed by =>, e.g. x: => T . The type of
such a parameter is then the parameterless method type => T . This indicates that
the corresponding argument is not evaluated at the point of function application,
but instead is evaluated at each use within the function. That is, the argument is
evaluated using call-by-name.
Um das ganze ein wenig fasssbarer zu machen,
ein kleines Beispiel:
def nano() = {
println("Getting nano")
System.nanoTime
}
def printNano(t : Long) = {
println("In printNano")
println("Param : " + t)
t
}
println(printNano(nano()))
Erwarteter Output auf der Console ->
Getting nano
In printNano
Param : 78383711083965
78383711083965
Oder so ähnlich
Nun mit einem Call-By-Name ->
def nano() = {
println("Getting nano")
System.nanoTime
}
def printNano(t: => Long) = {
println("In printNano")
println("Param : " + t)
t
}
println(printNano(nano()))
Man beachte, dass es nur eine kleine Winzigkeit von Unterschied im Code gibt =>
Die Methode nano wird nun zweimal aufgrufen, und zwar immer an den Stellen, wo eine Auswertung der Variablen t stattfindet. Es wird eine Parameterlose Funktion übergeben!
Output:
In printNano
Getting nano
Param : 78424935617021
Getting nano
78424964560567
Gruß JJR
def nano() = {
println("Getting nano")
System.nanoTime
}
def printNano(t : Long) = {
println("In printNano")
println("Param : " + t)
t
}
println(printNano(nano()))
Erwarteter Output auf der Console ->
Getting nano
In printNano
Param : 78383711083965
78383711083965
Oder so ähnlich
Nun mit einem Call-By-Name ->
def nano() = {
println("Getting nano")
System.nanoTime
}
def printNano(t: => Long) = {
println("In printNano")
println("Param : " + t)
t
}
println(printNano(nano()))
Man beachte, dass es nur eine kleine Winzigkeit von Unterschied im Code gibt =>
Die Methode nano wird nun zweimal aufgrufen, und zwar immer an den Stellen, wo eine Auswertung der Variablen t stattfindet. Es wird eine Parameterlose Funktion übergeben!
Output:
In printNano
Getting nano
Param : 78424935617021
Getting nano
78424964560567
Gruß JJR