callbyname

Difference between Call-by-name and Call-by-macro-expansion

╄→尐↘猪︶ㄣ 提交于 2020-01-16 16:21:46
问题 Let's assume we have the following code in a language that looks a lot like C. int A[2]; A[0]=4; A[1]=7; void f(int x, int y) { x++; A[1]++; y++; printf(x, y, A[0], A[1]); } void main() { int k = 0; f(k, A[k]); print(k, A[0], A[1]); } I want to define the output of this program. I haven't understood well the difference between the call-by-name and the call-by-macro-expansion method. So, in the call-by-name method, k is initialized to 0 and then f() function is called. x becomes equal to "k"

parameter list (“*”) with lazy “by-name” parameters?

孤街醉人 提交于 2019-12-22 14:15:22
问题 I can: scala> def foo( f: => String) = println(f) foo: (f: => String)Unit and I can: scala> def foo( f: String*) = f.map(println) foo: (f: String*)Seq[Unit] but I can't: scala> def foo( f: =>String* ) = f.map(println) <console>:1: error: ')' expected but identifier found. def foo( f: =>String* ) = f.map(println) ^ nor scala> def foo( f: (=>String)* ) = f.map(println) <console>:1: error: no by-name parameter type allowed here def foo( f: (=>String)* ) = f.map(println) ^ Is there some other way

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

纵饮孤独 提交于 2019-12-22 08:10:51
问题 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 any name for it? Or some documentation to describe the type? Further questions raised by answer Question 1 (When reading the spec of §3.3.1 (MethodTypes)) Method type is the type of a method, say I defined a method hello : def hello: String = "abc"

Scala case class prohibits call-by-name parameters?

青春壹個敷衍的年華 提交于 2019-12-18 12:47:38
问题 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 that call-by-name is not allowed. I've heard that it is because val or var constructor parameter is not allowed for call-by-name . For example: class A(val x: =>Int) //error: `val' parameters may not be call-by-name But in contrast the normal

Handling by-name parameters in Scala macro

余生颓废 提交于 2019-12-13 17:06:33
问题 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 paramTpes = f.tpe match { case pmt: PolyType if pmt.paramLists.size == 0 => Seq() case pmt: PolyType => pmt.paramLists(0) map {_.typeSignature .substituteTypes(pmt.typeParams, targs map (_.tpe))} case pmt: MethodType if pmt.paramLists.size == 0 => Seq() case pmt

Visual Basic: CallByName with multiple “Levels”

我怕爱的太早我们不能终老 提交于 2019-12-12 06:48:18
问题 I've been working on this for quite a bit now but I still cannot get my code going. I have a class called Contact which has a read-only property Address . The Address class has properties like Street and City . Now I want to assign a contact's street like this: CallByName(contact, "Address.Street", CallType.Set, new_street) But I am getting an error saying "Address.Street" is not a member of Contact . I need to set the property via its name so contact.Adress.Street = new_street is not an

Why `def hello[T](f: => T) = f; hello(()=>12)` is compilable but `def hello(f: => Int) = f; hello(()=>12)` is not?

眉间皱痕 提交于 2019-12-11 11:38:08
问题 Following code can be compiled: def hello[T](f: => T) = f hello(() => 12) But following not: def hello(f: => Int) = f hello(() => 12) Which reports error: <console>:9: error: type mismatch; found : () => Int required: Int hello(() => 12) Why? 回答1: I would say because T can be any () => x , but Int can't be a () => x . In your situation you are passing () => 12 as parameter which is a legal action because T has no constrains and can be anything, in fact doing so returns a partial applied

Scala call-by-name constructor parameter in implicit class

空扰寡人 提交于 2019-12-11 04:09:16
问题 The following code does not compile . Desired is to have a call-by-name constructor parameter in an implicit class as illustrated here, def f(n: Int) = (1 to n) product implicit class RichElapsed[A](val f: => A) extends AnyVal { def elapsed(): (A, Double) = { val start = System.nanoTime() val res = f val end = System.nanoTime() (res, (end-start)/1e6) } } where a call val (res, time) = f(3).elapsed res: Int = 6 time: Double = 123.0 This error is reported in REPL, <console>:1: error: `val'

parameter list (“*”) with lazy “by-name” parameters?

岁酱吖の 提交于 2019-12-06 09:57:42
I can: scala> def foo( f: => String) = println(f) foo: (f: => String)Unit and I can: scala> def foo( f: String*) = f.map(println) foo: (f: String*)Seq[Unit] but I can't: scala> def foo( f: =>String* ) = f.map(println) <console>:1: error: ')' expected but identifier found. def foo( f: =>String* ) = f.map(println) ^ nor scala> def foo( f: (=>String)* ) = f.map(println) <console>:1: error: no by-name parameter type allowed here def foo( f: (=>String)* ) = f.map(println) ^ Is there some other way to do what I want? Why isn't this allowed? Here the => means that the parameter is passed by name.

Example of Call by name

自作多情 提交于 2019-12-05 20:05:07
问题 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? 回答1: I'll work in a hypothetical programming language. Let's assume we have a function p(x) that