What are the differences between f: => R
and f: () => R
?
They seem to work in the same way, but their types are incompatible with each other.
It's completly different things: f: => R
- it's a type of function parameter (very specific - see this discussion), f: () => R
- it is a simple funtion.
They seem to work in the same way - because they realy work the same way - they calculated every time you call them. Call-by-name parameters have the advantage that they are not evaluated if they aren’t used in the function body (and that they calculated when you use them in function, not when you declare them or call them - only when evaluated that part of function where you use this specific params). Also see this (official description from scala-lang.org)