0

What is the most 'scala-ic' way to capture a value (possibly one that is not idempotent) for logging and returning the same value.

I can think of 'return' statement the only way to do it, but apparently using 'return' should be avoided in scala .

Use case:

def myfunc(argument) : ReturnType{
       val response:ReturnType = dependency()
       // dependency() is not idemptotent
       // so calling more than once will have side-effects
       logger.debug(response.member1 ,  response.member2)
       return response
}

Is there a way to achieve this without using a 'return' keyword.

I am a newbie to scala so some (or most) of what I said could be wrong, and would be happy to be corrected.

user462455
  • 12,838
  • 18
  • 65
  • 96
  • 2
    Just drop the word `return` - the result of evaluating the last expression is automatically returned as the function's result, so just `response` on its own will work. – Shadowlands Nov 16 '15 at 21:47
  • 2
    Not quite sure what you're asking, but you might be after the kestrel pattern: http://stackoverflow.com/questions/23231509/what-is-the-added-value-of-the-kestrel-functional-programming-design-pattern-s – The Archetypal Paul Nov 16 '15 at 22:04

1 Answers1

1

Just reifying @Shadowlands answer.

def myfunc(argument: ArgType): ReturnType {
   val response = dependency()
   logger.debug(response.member1, response.member2)
   response
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
seanmcl
  • 9,740
  • 3
  • 39
  • 45
  • Thanks! Is there a reason to avoid 'return' keyword in this case. Seems like it makes code more readable (my subjective opinion) with return statement. – user462455 Nov 16 '15 at 23:02
  • @user462455, It makes it more 'readable' mostly because you're used to it. Stop using it for a while and you probably won't miss it. – Chirlo Nov 17 '15 at 09:34