2

I know how scala by-name parameters work: https://tpolecat.github.io/2014/06/26/call-by-name.html

I'm using it in a very sensitive piece of code that gets run a lot. My questions is: Is there any performance or memory drawback? For example, I know using lazy val has its drawbacks: What's the (hidden) cost of Scala's lazy val?

Is there something similar about by-name parameters? How are they implemented underneath?

Please note I will NOT be using it with lazy for caching. So I wouldn't have that above mentioned problem. I just need to know under the hood it's not using lazy itself.

Thanks

Community
  • 1
  • 1
user1819676
  • 369
  • 1
  • 12
  • well... they are similar to functions... and are often combined with lazy... – sarveshseri Apr 28 '17 at 16:55
  • Updated to post to clarify, I will not use lazy val for caching it. I will use a var. So wouldn't have the lazy problem. Just need to know how it's implemented under the hood and if there is any issue with it. – user1819676 Apr 28 '17 at 16:58

1 Answers1

7

By name parameters are implemented as instances of Function1. So they also have the same performance characteristics.

Calling a method with a by name parameter has the overhead of creating an instance of Function1 and using the by name parameter has the overhead of calling the method apply on a Function1 object.

Jasper-M
  • 14,966
  • 2
  • 26
  • 37