Scala implicit parameter and Japanese emoticon 'foldLeft'
I am currently learning Scala, but there are still so many things I just don't understand ...
So, I was accidentally looking for a stack overflow when the wild answer popped up : https://stackoverflow.com/a/446873/stackoverflow
This answer uses an implicit parameter and a foldLeft function with a - yes it looks like one - a Japanese smiley face. source code of question:
class Account(implicit transactionLog: TransactionLog) {
def balance = transactionLog.foldLeft(_ + _)
}
class TransactionSlip(from: Account, to: Account, amount: BigDecimal)
What exactly does the implicit keyword mean in this case? What does an emoticon do? I just don't get it from the fragments floating around ...
Thanks in Advance.
source to share
Two answers:
-
_ + _
is a placeholder for a function that takes two arguments and adds them . The underscore is here to mark the position of the argument in such syntax. You can read this for all kinds of underscore uses in Scala. -
the keyword
implicit
denotes an implicit argument . This means that in the places where you needAccount
, you can provide a constructor without explicitly specifying the corresponding onetransactionLog
, which will be grabbed from the context.
source to share