Questions tagged [pass-by-name]
45 questions
0
votes
1 answer
What does : => A syntax mean in method parameter declaration?
So, reading the Scala tour about implicit classes in Scala, I came across this piece of code:
object Helpers {
implicit class IntWithTimes(x: Int) {
def times[A](f: => A): Unit = {
def loop(current: Int): Unit =
if(current > 0)…

Lucas Lima
- 832
- 11
- 23
0
votes
1 answer
Evaluation of variable through pass-by-name
I am learning parameter passing.
I've been working on a question about parameter passing, but I don't understand one problem.
I searched the Internet, but there was no example of a parameter being expression.
If it's a expression, can I know how to…

student12343
- 1
- 2
0
votes
2 answers
Is a while loop already implemented with pass-by-name parameters? : Scala
The Scala Tour Of Scala docs explain pass-by-name parameters using a whileLoop function as an example.
def whileLoop(condition: => Boolean)(body: => Unit): Unit =
if (condition) {
body
whileLoop(condition)(body)
}
var i = 2
whileLoop…

Chris
- 785
- 10
- 24
0
votes
2 answers
By-name parameter in Scala
From book 'Programming in Scala':
var assertionsEnabled = true
def myAssert(predicate: () => Boolean) =
if (assertionsEnabled && !predicate())
throw new AssertionError
myAssert(() => 5 > 3)
Using empty parameter list is awkward.
Scala provides…

Mandroid
- 6,200
- 12
- 64
- 134
0
votes
1 answer
What triggers Try argument to be evaluated
I was tinkering with Try, per this:
val files = Stream("a.txt", "b.txt", "c.txt")
files
.map(s => Try(Source.fromFile(s)))
.flatMap(t => t match {
case Failure(x) =>
Console.err.println(s"Failure: x.getMessage")
…

Toby Eggitt
- 1,806
- 19
- 24
0
votes
1 answer
How would call a function that calls "by-name" as an argument in scala
I'm pretty new to scala and still in the early days of learning. I was reading an article that had an example like so:
def example(_list: List[Positions], function: Position => Option[Path]): Option[Path] = _list match {...}
NB
Position is a…

Kacy
- 3
- 1
0
votes
2 answers
Pass-by-name implementation in C
I know that C uses pass-by-value and we can emulate pass-by-reference with the help of pointers. But, for example, in order to calculate a simple mathematical expression, how do I implement pass-by-name (which is kind of lazy evaluation but not…

phoeNix
- 63
- 1
- 8
0
votes
1 answer
Difference between call by name and call by text parameter passing mechanism with examples
Out of 5 types of parameter passing mechanism:
1.pass-by-value
2.pass-by-reference
3.pass-by-value-result
4.pass-by-text (macros in C)
5.pass-by-name (something like continuations)
I just want the difference between the last two.Please help…

Deepaank
- 1
- 1
0
votes
3 answers
Determine by parameter which member to change
Following Scala code gives a compile error stating I cannot assign to a val:
Simplified example:
class State {
val a = 1
val b = 2
def compute( res: =>Int, add : Int ): Unit = {
res = add + 123456
}
compute(a,b)
…

Suma
- 33,181
- 16
- 123
- 191
0
votes
1 answer
Reference type confusing in Go language
I tried to make Trie data structures by Go Language, but somehow it stuck with References problem,
Here it is. http://play.golang.org/p/ASSGF5Oe9R
// Package main provides ...
package main
import "fmt"
type RootTrie []Trie
type Trie struct {
…

tomahawk28
- 87
- 4
- 7
0
votes
1 answer
Use a stored value in the superclass' constructor when extending a class taking a by-name parameter
I have a case where I wish to extend a class that takes by-name parameter in it's constructor:
class Extension(something: Something)
extends Base(something.doSomething(something.getSomething(false))
class Base(expression: => Result) {
…

Gareth Latty
- 86,389
- 17
- 178
- 183
0
votes
1 answer
Call by name (normal order evaluation)
I have the following code below (C like language). I know how to trace if pass-by-value or pass-by-reference was used, but there is also call-by-name mechanism. Can anyone guide me how to trace call-by-name?
int x=12,y=10;
void tswap(int pa, int pb)…

user2878007
- 409
- 2
- 5
- 19
0
votes
0 answers
Scala collection that accepts pass-by-name parameters
Does such collection exist?
At the moment the code I have doesn't compile:
object pagerank {
// TODO fixme
val totalNodes: BigDecimal = 4
class Node(in: => List[Node], out: => List[Node]) {
def rank: BigDecimal = {
…

Anton Kuzmin
- 821
- 1
- 10
- 26
-1
votes
1 answer
Output for pass-by-value and pass-by-name
I am needing to find the result of the following code when x and y are passed-by-value and also when passed-by-name.
PROGRAM EX1;
int i; //global
int A[3]; //global
PROCEDURE P1(int x, int y)
Begin
y:=2;
PRINT(x);
…

KNuz
- 1
- 1
-2
votes
1 answer
output of a function which has passByName parameter passing paradigm
I've tried to figure out what is the output of a code like this .By the way ,It is not a real question, kind of therical question, i mean it is not an original c code, it is kind of a PL having c-code syntax and passed by name parameter…

caesar_
- 261
- 1
- 2
- 8