Questions tagged [callbyname]

86 questions
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
  • 193,756
  • 157
  • 432
  • 708
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…
6harat
  • 542
  • 6
  • 18
3
votes
2 answers

Non-strict by-name arguments in Python?

Question Is there any way to declare function arguments as non-strict (passed by-name)? If this is not possible directly: are there any helper functions or decorators that help me achieve something similar? Concrete example Here is a littly…
Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
3
votes
2 answers

Understanding Call-By-Name in Scala

I am new to scala language, therefore I will be grateful if someone could explain me this code-snippet : object C { def main(args: Array[String]) = { measure("Scala"){ println("Hello Back") } } def measure(x: String)(y: =>…
sasuke
  • 145
  • 1
  • 10
3
votes
2 answers

Tunnel implicit parameter to call-by-name function body

Consider following code snippet: object Example { def run(f: => Unit): Unit = { implicit val i = 1 f } def caller(): Unit = run { todo } def todo(implicit i: Int): Unit = …
llirik
  • 148
  • 7
3
votes
1 answer

Overloading function with call-by-name parameter and function with by-value parameter

Why does doSmth(() => s) not compile? Why does the rest of the code output "value"? Is there a way to call the second function(with call-by-name parameter)? object Test { def main (args: Array[String]){ lazy val s: String = "" doSmth(s) …
ilinum
  • 464
  • 4
  • 11
3
votes
1 answer

Using apply ("()") on function passed in by-name parameter: evaluation is not forced?

I have a function: def nanoTime() = { println("Getting nano time...") System.nanoTime // returns nanoTime } and another function, which takes a function def printTime(time: => Long) = { // indicates a by-name parameter println(">>…
dublintech
  • 16,815
  • 29
  • 84
  • 115
2
votes
1 answer

How good is Scala By-name parameters performance?

I know how scala by-name parameters work: https://tpolecat.github.io/2014/06/26/call-by-name.html I'm using it in a very sensitive piece of code that gets run a lot. My questions is: Is there any performance or memory drawback? For example, I know…
user1819676
  • 369
  • 1
  • 12
2
votes
2 answers

Performance difference between def and val

Consider the below code where I am passing a method and a function as a parameter to map() val list1:List[Int]=List(10,20,30) def func1(x:Int):Int={ x+10 } list1.map(func1) list1.map(_+10) I have few questions about ETA…
codingsplash
  • 4,785
  • 12
  • 51
  • 90
2
votes
1 answer

Call-by-value and by-name equivalence

I'm working in a Coursera course on functional programming and at some point they discuss the difference between call-by-value and call-by-name evaluation techniques. They're some point that confuses me, they say: Both techniques reduce to the same…
2
votes
1 answer

What would this print under call-by-name and call-by-value?

I have this line of code in a toy-language. The print-function takes a list of arguments and prints those. print(a, (a := 5, a)) Would there be a difference in the output if I used call-by-value or call-by-name? If so, what would the outputs be. It…
2
votes
2 answers

Pass by name implementation in C

How can I calculate the value of the arithmetic expression ^2 + 3i − 1 that is dependent on the index i by using pass-by-name mechanism in C language 9 ∑ i^2 + 3i − 1 =0 through a call to a sum procedure with argument(s) passed by name Pass by name…
2
votes
0 answers

Scala by name versus function parameters

This is a named parameter: def foo(bar: => Boolean): Boolean = bar And this is a function parameter: def foo(bar: () => Boolean): Boolean = bar() How do the two declarations differ? Both will be evaluated lazily and each time the parameter is…
Saish
  • 1,518
  • 1
  • 12
  • 21
2
votes
1 answer

Macro variant of implicit class that allows for by-name argument

For a DSL, I want to introduce a dup extension method that basically calls Vector.fill, e.g. import scala.collection.immutable.{IndexedSeq => Vec} implicit final class Dup[A](private val in: A) extends AnyVal { def dup(n: Int): Vec[A] =…
0__
  • 66,707
  • 21
  • 171
  • 266
2
votes
1 answer

Handling by-name parameters in Scala macro

I have a macro that does some analysis on nested function applications. It matches applications and retrieve the parameter types this way: case q"$f[..$targs](..$args)(...$otherArgs)" => // retrieve the list of all parameter types val…
Lionel Parreaux
  • 1,115
  • 1
  • 9
  • 22