|
|
Re: Why are "by-name" parameters named as such?
|
Posted: Apr 13, 2008 8:07 AM
|
|
> I have a feeling that this has something to do with its > difference to a call "by-value" parameter. > > Wikipedia has a good article: > http://en.wikipedia.org/wiki/Evaluation_strategy
Thanks Callum. Your explanation seems to fit. (just thinking aloud below)
// by-value def fbyvalue(op: Int) = ... // by-name def fbyname(op: => Int) = ...
// calling f(x)s defined above fbyvalue(3 + 5) fbyvalue gets 8
fbyname(3 + 5) fbyname gets 3 + 5
Both fbyvalue() and fbyname() are expecting an integer value. The difference is that the expression that you pass to fbyvalue() is evaluated before fbyvalue() is called, while the expression passed to fbyname() is evaluated within the fbyname().
Of course, that is the difference in evaluation, which is obvious, but that does not explain the name "by-name". But Callum pointed out that it is called so in contrast to the name "by-value".
It does seem clear now, where Scala gets "by-name" for its by-name parameters.
Although, it is still a bit mystifying why it is called "by-name", when something like "by-substitution" would be more self-evident. I suppose "by-name" is shorter and has been "chosen" by CS gurus.
|
|