Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this idiom Scala. Making a def to shorten a statement?

I'm trying to avoid typing long sentences in the parameter list.
Is this an idiom Scala way to archive that?

  def createRecaptchaHtml: String = {
    def config(s: String) = Play.current.configuration.getString(s).toString()
    ReCaptchaFactory.newReCaptcha(config("recaptcha.publicKey") , config("recaptcha.privateKey"), false).createRecaptchaHtml(null, null)
like image 548
Farmor Avatar asked Apr 13 '12 16:04

Farmor


People also ask

What is an expression in Scala?

2.2. In Scala, all statements are expressions that return a value. The value of the last statement determines the value of an expression. The if expression is no different — it always returns a value. So, we can assign the result of an if expression to a value.

What is a Scala method?

A Scala method is a part of a class which has a name, a signature, optionally some annotations, and some bytecode where as a function in Scala is a complete object which can be assigned to a variable. In other words, a function, which is defined as a member of some object, is called a method.

Which of the following expressions has side effects Scala?

Void Function A function returns nothing, such as void , implies there is a side effect.


1 Answers

Yes, this kind of local methods are perfect for that application. An alternative is to import the instance methods you need in the scope:

def createRecaptchaHtml: String = {
  import Play.current.configuration.getString
  ReCaptchaFactory.newReCaptcha(
    getString("recaptcha.publicKey").get,
    getString("recaptcha.privateKey").get, 
    false
  ).createRecaptchaHtml(null, null)
}
like image 109
paradigmatic Avatar answered Sep 18 '22 00:09

paradigmatic