Questions tagged [callbyname]
86 questions
27
votes
4 answers
Scala case class prohibits call-by-name parameters?
Scenario:
I want to implement an infinite list:
abstract class MyList[+T]
case object MyNil extends MyList[Nothing]
case class MyNode[T](h:T,t: => MyList[T]) extends MyList[T]
//error: `val' parameters may not be call-by-name
Problem:
The error is…

WeiChing 林煒清
- 4,452
- 3
- 30
- 65
11
votes
2 answers
Why does Scala evaluate the argument for a call-by-name parameter if the method is infix and right-associative?
As I understood call-by-name parameters of a method, the corresponding argument expression will not be evaluated when passing it to the method, but only when (and if) the value of the parameter is used in the method body.
In the following example,…

Holger Peine
- 1,079
- 1
- 9
- 13
11
votes
1 answer
How to mock a call-by-name argument (like getOrElse) using ScalaMock?
I'd like to be able to mock a return value of getOrElse method so that it returns what is passed as orElse call-by-name argument with ScalaMock
trait ToBeMocked {
def getOrElse(arg: Int)(orElse: => String): String
}
ScalaMock has to be used…

Sergei Poganshev
- 170
- 1
- 8
10
votes
1 answer
Parameterized logging in slf4j - how does it compare to scala's by-name parameters?
Here are two statements that seem to be generally accepted, but that I can't really get over:
1) Scala's by-name params gracefully replace the ever-so-annoying log4j usage pattern:
if (l.isDebugEnabled() ) {
logger.debug("expensive string…

teo
- 1,393
- 1
- 15
- 25
9
votes
3 answers
Example of Call by name
In my principles of programming class we are talking about different calling methods. Some we discussed were:
call by value
call by reference
call by value/result
and call by name
I can't find an example of how call by name works. Anyone care to…

sixtyfootersdude
- 25,859
- 43
- 145
- 213
8
votes
2 answers
performance in scala logging libraries call-by-value vs call-by-name
I've been looking at the various scala logging libraries lately, and the vast majority of them implement their logging functions as
def debug(s: => String)
So that if you have debug logging turned off, it won't execute the statement. However, I…

Falmarri
- 47,727
- 41
- 151
- 191
7
votes
1 answer
Scala implicit conversion on call-by-name parameter works differently depending on the function is overloaded or not
Let's see the code below:
import scala.language.implicitConversions
class Foo
implicit def int2Foo(a: => Int): Foo = new Foo
def bar(foo: Foo) = {}
def bar(foo: Boolean) = {}
bar {
println("Hello")
64
}
This code does not print anything,…

pocorall
- 1,247
- 1
- 10
- 24
6
votes
2 answers
How do I pass an array of arguments ByRef with CallByName?
I am currently using CallByName to dynamically call methods. There are several methods which I pick up daily from a table in server along with the arguments. For this reason, I send an array of the arguments to CallByName rather than a param array…

maracuja
- 417
- 8
- 24
5
votes
5 answers
How to implement call by name in C#?
Can anyone tell me how I can implement Call By Name in C#?

Dr TJ
- 3,241
- 2
- 35
- 51
5
votes
2 answers
In Scala, when would be a good time to use lazily evaluated parameter rather than to use a function as a parameter?
def getStr(): String = {
println("getStr is running")
"str"
}
def lazyHello(para: => String) = {
println("lazy hello is runing")
println(para)
}
def notLazyHello(para: String) = {
println("not lazy hello is runing")
…

Cui Pengfei 崔鹏飞
- 8,017
- 6
- 46
- 87
5
votes
1 answer
Perl: Problems calling subroutines by reference using a hash value
In Perl, you are able to call a function by reference (or name) like so:
my $functionName = 'someFunction';
&$functionName();
#someFunction defined here:
sub someFunction { print "Hello World!"; }
What I am trying to do is use a…

Thumper
- 525
- 1
- 6
- 21
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
2 answers
Passing an array of Arguments to CallByName VBA
I'm using callByName I VBA to dynamically call different methods of a class. Depending on the method, I will have a different number of arguments which will be held in an array. Unfortunately CallByName accepts a param array, therefore it's not…

maracuja
- 417
- 8
- 24
4
votes
1 answer
Scala: lazy vals, call by name, closures and memory leaks
I have a scala procedure creating a large data structure using an even larger index in the process. Because I want to do it in one pass and not get boggled down in complicated precedence resolution, I'm using lazy vals in the result initialized with…

Turin
- 2,208
- 15
- 23
4
votes
2 answers
Difference between call-by-value and call-by-name interpreter for the lambda calculus
In another question, Bob presented the following interpreter for the untyped lambda calculus.
data Expr = Var String | Lam String Expr | App Expr Expr
data Value a = V a | F (Value a -> Value a)
interpret :: [(String, Value a)] -> Expr -> Value…

Cirdec
- 24,019
- 2
- 50
- 100