Questions tagged [pass-by-name]
45 questions
47
votes
8 answers
What is "pass-by-name" and how does it work exactly?
I've checked Wikipedia and googled but I still can't wrap my mind around how pass-by-name works in ALGOL 60.

bdd
- 3,436
- 5
- 31
- 43
40
votes
3 answers
Python: Passing parameters by name along with kwargs
In python we can do this:
def myFun1(one = '1', two = '2'):
...
Then we can call the function and pass the arguments by their name:
myFun1(two = 'two', one = 'one')
Also, we can do this:
def myFun2(**kwargs):
print kwargs.get('one',…

CodeArtist
- 5,534
- 8
- 40
- 65
17
votes
13 answers
Consequences of only using stack in C++
Lets say I know a guy who is new to C++. He does not pass around pointers (rightly so) but he refuses to pass by reference. He uses pass by value always. Reason being that he feels that "passing objects by reference is a sign of a broken…

BefittingTheorem
- 10,459
- 15
- 69
- 96
9
votes
2 answers
Why is Scala's behavior in case of overloading with by-name parameters different from the case with by-value parameters?
Given this Scala code:
object test {
def byval(a: Int) = println("Int")
def byval(a: Long) = println("Long")
def byname(a: => Int) = println("=> Int")
def byname(a: => Long) = println("=> Long")
def main(args: Array[String]) {
…

Jean-Philippe Pellet
- 59,296
- 21
- 173
- 234
8
votes
6 answers
Call by reference, value, and name
I'm trying to understand the conceptual difference between call by reference, value, and name.
So I have the following pseudocode:
foo(a, b, c)
{
b =b++;
a = a++;
c = a + b*10
}
X=1;
Y=2;
Z=3;
foo(X, Y+2, Z);
What's X, Y, and Z after the…

workinMan
- 198
- 1
- 2
- 9
6
votes
0 answers
What does A => => B mean?
In another question I ended up with a value of type A => => B. What does this mean?
I've always been led to understand that => A is not actually a type, but a way an argument can be declared to work with call-by-name semantics. But if it's not a…

Martijn
- 11,964
- 12
- 50
- 96
6
votes
1 answer
By-name type parameters
Imagine I have the following class definition:
class Foo[T]
and I want to do the following
def bar(x:Foo[ =>Int ]):Int = ???
But compiler fails with "no by-name parameter type allowed here"
How can I use a by-name type as type parameter for a…

Sane
- 2,334
- 2
- 17
- 20
5
votes
1 answer
Why does this Iterator infinitely loop?
I'm attempting to chain Iterators:
var it = Iterator(1)
it.next
it = Iterator(2) ++ it
it.next
it.hasNext
This infinitely loops on the hasNext as you can see here: https://scastie.scala-lang.org/qbHIVfsFSNO5OYmT4pkutA
If you run this and inspect…

Jack Koenig
- 5,840
- 15
- 21
5
votes
4 answers
Use of Scala by-name parameters
I am going through the book "Functional Programming in Scala" and have run across an example that I don't fully understand.
In the chapter on strictness/laziness the authors describe the construction of Streams and have code like this:
sealed trait…

melston
- 2,198
- 22
- 39
5
votes
1 answer
Hiding and scoping implicit variable creation in a DSL
When developing a DSL, what is the cleanest way to limit the scope of an implicit variable and simultaneously hide the fact that such an implicit variable is defined?
As an example, this is the desired behavior...
object External
{
def…

Matt
- 53
- 2
4
votes
2 answers
How to set a do-nothing handler to a by-name parameter?
I defined a method treeNode to create a node, and which can have children nodes. The simplified code is:
def treeNode(text:String) (children: => Any) {
val b = new TreeNode(text)
children
}
When I use this method, I have to…

Freewind
- 193,756
- 157
- 432
- 708
4
votes
0 answers
From `=> T` to `() => T` and back again
As parameters of types => T and () => T are both Function0, I always naively assumed that conversion from one to another is a noop. It seems however, that while true for (=>T) => (() => T), the other direction (() => T) => (=> T) results in wrapping…

Turin
- 2,208
- 15
- 23
4
votes
1 answer
C++ elegant way of passing (a subset of) options to a function
I have a function whose exact behaviour is controlled by a set of flags. Most of these are usually false. Having them all as parameters quickly becomes very messy.
I could create an enum for the flags:
enum ParseFlags { assignmentPossible = 1,…

Felix Dombek
- 13,664
- 17
- 79
- 131
4
votes
1 answer
Scala function not passed by name with curly brackets (braces)
I would like to pass a function by name do something before its execution. Consider the following example:
class Runner {
def apply(procedure: => Unit) = {
println("running procedure")
procedure
}
}
new Runner()(println("procedure!"))…

Mifeet
- 12,949
- 5
- 60
- 108
3
votes
3 answers
By-Name-Parameters for Constructors
coming from my other question is there a way to get by-name-parameters for constructors working? I need a way to provide a code-block which is executed on-demand/lazy/by-name inside an object and this code-block must be able to access the…

hotzen
- 2,800
- 1
- 28
- 42