-
Notifications
You must be signed in to change notification settings - Fork 21
add lazy parameters #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Imported From: https://issues.scala-lang.org/browse/SI-240?orig=1
|
Matthew Pocock (drdozer) said: Either way, I would like to help out if I can with implementing this. My particular use-case is in building lazy, infinite case-class structures such as this rather boring one: case class Foo(lazy data: Foo) I presume that any patch that adds lazy function parameters would out of the box, or with minor tweaking, support this also. |
@hubertp said: |
Matthew Pocock (drdozer) said: case class Foo(lazy data: Foo) foo match { The second case needs to not evaluate data. If it does, then it will be impossible to pattern-match against (portions of) infinite data structures. |
@jrudolph said: jrudolph/scala-starrless@65052ce It's still rough:
|
@jrudolph said: |
@paulp said: |
Ryan Hendrickson (ryan.hendrickson_bwater) said: Additional desired syntax, if this is supported in (primary) constructors: class Stuff([[mods]] lazy val a: Int, [[mods]] def b: Int) translating to (modulo named-argument issues): class Stuff(a': => Int, b': => Int) {
[[mods]] lazy val a: Int = a'
[[mods]] def b: Int = b'
} |
@retronym said: To get the ball rolling: what would be expected here? def foo(lazy a: Int)(b: Int = a) { a }
foo({println("!"); 0})() |
Matthew Pocock (drdozer) said (edited on May 2, 2013 9:07:32 PM UTC): def foo(lazy a: Int)(b: Int = a) { println("<") ; a; println(">") }
def bar(lazy a: Int): (Int) => Unit = (b: Int = a) => {println("<") ; a; println(">"); ()} With the 'evaluate on first de-reference' semantics of lazy, I'd expect this to run as follows: foo({println("!"; 42})()
| !<>
foo({println("!"; 42})(13)
| <!> However, I'd see an argument for excluding lazy arguments from default values lists, as this least-surprise semantics will require some fancy re-writes that I can't imagine how to get right in the presence of currying. |
Ryan Hendrickson (ryan.hendrickson_bwater) said: def foo(a: Int)(b: Int = a) { ... }
foo(effectfulCode)() rewrites to (I forget the exact mangling, but roughly) def foo(a: Int)(b: Int) { ... }
def foo$default2(a: Int): Int = a
val x$1: Int = effectfulCode
foo(x$1)(foo$default2(x$1)) , I would expect def foo(lazy a: Int)(b: Int = a) { ... }
foo(effectfulCode)() to rewrite to def foo(a: => Int)(b: Int) { ... }
def foo$default2(a: => Int): Int = a
lazy val x$1: Int = effectfulCode
foo(x$1)(foo$default2(x$1)) Wouldn't that have the desired semantics? |
@retronym said: We've been burned so often by unforseen (although not unforseeable) feature interactions (think Value Classes). So I'm naturally cautious with this sort of thing. |
Ryan Hendrickson (ryan.hendrickson_bwater) said: It is a good point about the binary-but-not-really compatibility of a function taking |
@jrudolph said: |
Ryan Hendrickson (ryan.hendrickson_bwater) said: Also, consider eta-expansion: def foo(lazy a: Int) = { a; a }
val f = foo _ f should have type (=> Int) => Int (I think; unless we're reforming how call-by-name types work), but the lazy semantics should be preserved so that a is evaluated only once. Either the rewrite is internal, or the eta-expansion gets complicated. Similarly, can we allow anonymous functions to have lazy arguments, given that we already permit them to have call-by-name? val f: (=> Int) => Int = (lazy a) => { a; a } |
Ryan Hendrickson (ryan.hendrickson_bwater) said: |
I believe this is now out of scope for Scala 2, but there's a Scala 3 ticket: lampepfl/dotty-feature-requests#174 |
For now, we only have call-by-name parameters. We should allow the
lazy
modifier on parameters, for instance:The meaning should be equivalent to:
The text was updated successfully, but these errors were encountered: