pass-by-name

What does : => A syntax mean in method parameter declaration? [duplicate]

跟風遠走 提交于 2021-02-04 08:34:09
问题 This question already has answers here : By-name parameter vs anonymous function (5 answers) Closed 11 months ago . 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) { f loop(current - 1) } loop(x) } } } What is troubling me here is the def times[A](f: => A): Unit = { line. What is going on here? The part of the

By-Name-Parameters for Constructors

孤者浪人 提交于 2020-02-04 09:01:04
问题 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 class-methods as if the code-block were part of the class. Following Testcase fails: package test class ByNameCons(code: => Unit) { def exec() = { println("pre-code") code println("post-code") } def meth() = println("method") def exec2(code2: => Unit) = {

By-Name-Parameters for Constructors

蹲街弑〆低调 提交于 2020-02-04 09:01:01
问题 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 class-methods as if the code-block were part of the class. Following Testcase fails: package test class ByNameCons(code: => Unit) { def exec() = { println("pre-code") code println("post-code") } def meth() = println("method") def exec2(code2: => Unit) = {

Use of Scala by-name parameters

牧云@^-^@ 提交于 2019-12-18 08:26:16
问题 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 Stream[+A] case object Empty extends Stream[Nothing] case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A] object Stream { def cons[A](hd: => A, tl: => Stream[A]) : Stream[A] = { lazy val head = hd lazy val tail = tl Cons(() => head

By-name parameter in Scala

China☆狼群 提交于 2019-12-11 15:52:44
问题 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 by-name parameter to solve this. def byNameAssert(predicate: => Boolean) = if (assertionsEnabled && !predicate) throw new AssertionError byNameAssert(5 > 3) I have a confusion in this discussion.myAssert takes in a parameter which is a function, which

Why is Scala's behavior in case of overloading with by-name parameters different from the case with by-value parameters?

寵の児 提交于 2019-12-06 21:18:23
问题 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]) { byval(5) byname(5) } } the call byval(5) compiles correctly, but byname fails to compile: ambiguous reference to overloaded definition Why? I would expect to observe the same behavior for by-value and by-name parameters with respect to overloading… How can it be fixed?

Why is Scala's behavior in case of overloading with by-name parameters different from the case with by-value parameters?

柔情痞子 提交于 2019-12-05 02:33:34
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]) { byval(5) byname(5) } } the call byval(5) compiles correctly, but byname fails to compile: ambiguous reference to overloaded definition Why? I would expect to observe the same behavior for by-value and by-name parameters with respect to overloading… How can it be fixed? That's because JVM does not support a "by-name" parameter, so Scala has to implement it in another way.

Python: Passing parameters by name along with kwargs

。_饼干妹妹 提交于 2019-11-30 06:01:45
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', 'nothing here') myFun2(one='one') So I was wondering if it is possible to combine both methods like: def myFun3(name, lname, **other_info): ... myFun3(lname='Someone', name='myName', city='cityName', otherInfo='blah') In general what combinations can we do? Thanks and sorry for my silly question. The general idea is: def func(arg1, arg2, ..., kwarg1=default, kwarg2

Use of Scala by-name parameters

南楼画角 提交于 2019-11-29 14:10:22
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 Stream[+A] case object Empty extends Stream[Nothing] case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A] object Stream { def cons[A](hd: => A, tl: => Stream[A]) : Stream[A] = { lazy val head = hd lazy val tail = tl Cons(() => head, () => tail) } ... } The question I have is in the smart constructor ( cons ) where it calls the

Python: Passing parameters by name along with kwargs

人走茶凉 提交于 2019-11-29 05:07:01
问题 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', 'nothing here') myFun2(one='one') So I was wondering if it is possible to combine both methods like: def myFun3(name, lname, **other_info): ... myFun3(lname='Someone', name='myName', city='cityName', otherInfo='blah') In general what combinations can we do? Thanks