Questions tagged [callbyname]

83 questions
25
votes
3 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…
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,009
  • 1
  • 9
  • 13
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
  • 23,394
  • 39
  • 132
  • 200
9
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…
9
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,353
  • 1
  • 14
  • 24
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
  • 44,586
  • 38
  • 140
  • 186
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,007
  • 1
  • 8
  • 20
6
votes
1 answer

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
  • 170
  • 4
  • 18
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
  • 2,966
  • 31
  • 49
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") …
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
  • 518
  • 5
  • 19
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
  • 1,893
  • 11
  • 18
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
  • 23,492
  • 2
  • 45
  • 94
4
votes
1 answer

What's the type of `=> String` in scala?

In scala, there is some call-by-name parameters: def hello(who: => String) = println("hello, " + who) What's the type of the parameter who? It shows the function on scala REPL as: hello: (who: => String)Unit Is the type still => String? Is there…
Freewind
  • 177,284
  • 143
  • 381
  • 649
3
votes
2 answers

Can Scala's "call by name" be considered as a syntactic sugar of Java8's Functional Interface API?

Example of Scala's "call be name": def giveMeName(b: => String) Java result: public class some.package.CallByNameEx { public void giveMeName(scala.Function0); public some.package.CallByNameEx(); } Example of Java's…
Gulats
  • 432
  • 5
  • 11
1
2 3 4 5 6