DEV Community

Enrque\m/Talavera
Enrque\m/Talavera

Posted on

Chapter26

Suposse you have the following function

ifBothTrue(age >18)(numAccidentes==0){
println("Discount!");
}

Attempt no1:

def ifBothTtrue(c:=>Boolean)(n:=>Boolean)(codeBlock:=>Unit):Unit{
if (c && n) {
codeBlock
}

}

You're using by-name, because we need to evaluate that expressión, always (think the case for a loop iteration)

Implicit paremeters: it takes the current val defined in the context with the word 'implicit'.
Some rules are:
-You can only use implicit per function
-If you use implicit, is has to be the last parameter.

You need to supply empy parentheses when you want to use the default values

def fn(a:Int =1)(b:Int =2) = {a+b}
you need to call fn()()

It also valid to use:
def fn(a:Int =1)(b:Int=a) = {a=b}

Top comments (0)