Skip to content

Instantly share code, notes, and snippets.

@mrbrdo
Last active August 29, 2015 14:10
Show Gist options
  • Save mrbrdo/5d5ee91b0969fcd487fa to your computer and use it in GitHub Desktop.
Save mrbrdo/5d5ee91b0969fcd487fa to your computer and use it in GitHub Desktop.
def x: Int = { println("x()"); 1 }
def a(byName1: => Int, byName2: => Int): Unit = {
println("a()")
println(byName1 + byName2)
}
a(x * 2, x * 3)
// output (note when x is actually called):
// a()
// x()
// x()
// 5
// equivalent call in Ruby: a( proc { x * 2 }, proc { x * 3 } )
// but syntax in Ruby is too verbose to actually design APIs like this
// hard to implement in Ruby VM but easy "cheating" could be in method naming
// e.g. a@(x * 2, x * 3) or some other syntax sugar
// it would be ok since: SyntaxError: `@(' is not allowed as an instance variable name
// it might actually be even better because you know what will happen with your parameters
// when you call the method (because of different syntax in method call)
@matz
Copy link

matz commented Nov 30, 2014

This reminds me of C++ reference type, which I don't like at all.
The semantics of expression should be recognized at a glance.
In that sense, I don't like macros neither.

What do you use name parameter for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment