callbyname

Why does Scala evaluate the argument for a call-by-name parameter if the method is infix and right-associative?

我只是一个虾纸丫 提交于 2019-12-04 02:57:34
问题 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, however, this is only true in the first two method calls, but not in the third one, although it should be a merely syntactical variation of the second case!? Why is the argument expression evaluated in the third method call? (I tested this code

Example of Call by name

戏子无情 提交于 2019-12-04 02:39:04
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 give me an example? I think that when you take an xml file as input this is similar to call by name. Could someone give me a more traditional example? I'll work in a hypothetical programming language. Let's assume we have a function p(x) that prints out x and then returns it. Now let's define a function: function foo(x, y) { return y+1; } Now let's

Passing an array of Arguments to CallByName VBA

五迷三道 提交于 2019-12-02 11:45:25
问题 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 straightforward to pass a variable number. Is there a way around this, I found a solution using the Type Information Library but this does not seem to work on VBA even though I have added it as a reference. Below is an illustration of what I want

Why does Scala evaluate the argument for a call-by-name parameter if the method is infix and right-associative?

╄→гoц情女王★ 提交于 2019-12-01 16:28:06
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, however, this is only true in the first two method calls, but not in the third one, although it should be a merely syntactical variation of the second case!? Why is the argument expression evaluated in the third method call? (I tested this code using Scala 2.11.7) class Node(x: => Int) class Foo { def :: (x: =>Int) = new Node(x) // a right

How to mock a call-by-name argument (like getOrElse) using ScalaMock?

流过昼夜 提交于 2019-12-01 03:47:26
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 because of currying. Running this: class CallByNameMockSpec extends Specification with MockFactory { trait ToBeMocked { def getOrElse(arg: Int)(orElse: => String): String } "Mocking functions with call-by-name arguments" should { "work" in { val m = mock[ToBeMocked] (m.getOrElse (_: Int)(_: String)).expects(101, *).onCall((x, y) => y) m.getOrElse(101)(

How to mock a call-by-name argument (like getOrElse) using ScalaMock?

混江龙づ霸主 提交于 2019-12-01 01:44:24
问题 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 because of currying. Running this: class CallByNameMockSpec extends Specification with MockFactory { trait ToBeMocked { def getOrElse(arg: Int)(orElse: => String): String } "Mocking functions with call-by-name arguments" should { "work" in { val m = mock