The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Why are "by-name" parameters named as such?

3 replies on 1 page. Most recent reply: Apr 13, 2008 8:21 AM by Jayce Vaidyan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Jayce Vaidyan

Posts: 8
Nickname: finite42
Registered: Mar, 2007

Why are "by-name" parameters named as such? Posted: Mar 22, 2008 8:35 PM
Reply to this message Reply
Advertisement
quick description: by-name parameters allow a short-cut for functions that take 0 arguments, allowing the creation of custom control constructs (esp. when coupled with currying).

silly example:

def blah(action: => Boolean) {
println("Aha! " + action())
}


"by-name" makes me think of named parameters as found in Python and Perl (simulated using its flexible list syntax).

I can't think of what the "name" part means in the context of Scala's by-name parameters.

I tried the explanation that it is because we call this parameter by name to apply it, but that would apply to all function parameters.

I can't think of a short name for this either, so I suppose this is something that was made up mostly arbitrarily for a lack of a better name.

I googled without much success.

If there is an interesting story or explaination behind this, it could make an iteresting footnote for the e-book. :-)


Callum Rhodes

Posts: 3
Nickname: 54709
Registered: Mar, 2008

Re: Why are "by-name" parameters named as such? Posted: Mar 30, 2008 4:13 PM
Reply to this message Reply
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

Jayce Vaidyan

Posts: 8
Nickname: finite42
Registered: Mar, 2007

Re: Why are "by-name" parameters named as such? Posted: Apr 13, 2008 8:07 AM
Reply to this message Reply
> 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.

Jayce Vaidyan

Posts: 8
Nickname: finite42
Registered: Mar, 2007

Re: Why are "by-name" parameters named as such? Posted: Apr 13, 2008 8:21 AM
Reply to this message Reply
Going back and forth on the by-name issue, I just realized that Scala has implemented a different evaluation strategy without providing something like a pre-processing facility.

Scala has used it's closure/function-objects and a little bit of syntactic sugar to do this. That's really clever.

I kept comparing by-name parameters to function objects parameters:
f(x: => T)
vs.
f(x: () => T) ).

I should have been comparing by-name parameters to arbitrary value parameters
f(x: => T)
vs.
f(x: T) ).

Thanks again Callum.

Flat View: This topic has 3 replies on 1 page
Topic: Need clarification on closure example page 198 Previous Topic   Next Topic Topic: immutable HashMap

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use