Skip to content
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

Parameters should be allowed as by-name and repeated #237

Closed
scabug opened this issue Nov 13, 2007 · 12 comments
Closed

Parameters should be allowed as by-name and repeated #237

scabug opened this issue Nov 13, 2007 · 12 comments

Comments

@scabug
Copy link

scabug commented Nov 13, 2007

Compiling

def f(f: =>int*) = null

should be allowed.

@scabug
Copy link
Author

scabug commented Nov 13, 2007

Imported From: https://issues.scala-lang.org/browse/SI-237?orig=1
Reporter: @dubochet

@scabug
Copy link
Author

scabug commented Jan 14, 2009

@odersky said:
Milestone next_bugfix deleted

@scabug
Copy link
Author

scabug commented Jun 30, 2012

DaveScala (davescala) said (edited on Jun 30, 2012 5:38:39 PM UTC):
Threads associated with this issue:

http://www.scala-lang.org/node/4272

In constructor:
http://groups.google.com/group/scala-user/browse_thread/thread/268f3ed8736642dd

@scabug
Copy link
Author

scabug commented Jul 1, 2012

@jrudolph said:
I once started implementing this. As so often I didn't think too hard about how the solution would look like. At least since then I know that the problem here is that it is not clear what the semantics of this construct would be exactly. What should the actual type of the parameter look like: There are two principle possibilities: should it be Seq[Function0[T]] or Function0[Seq[T]]. The first one means, that each variable argument parameter would be by-name on its own and only evaluated when it is requested (but when is this?), the second possibility would mean that all the variable parameters are evaluated together once the sequence is referenced.
I think both possibilities could be desirable in some situations, it's unclear how to implement the first one (since the Function0 doesn't appear in the method explicitly, how do you specify when you want to access and evaluate a particular element?).
I think to really solve the issue one would have to decide for one possibility first and then make a good case why it is obvious that this case is chosen (or make it more obvious by requiring it syntactical e.g. by forcing def f(f: => (Int*)) so that it is clear that the by-name-ness is applied to the sequence of variable arguments as a whole (possibility 2).

@scabug
Copy link
Author

scabug commented Jul 1, 2012

@retronym said:
I think Function0[Seq[T]] is both more intuitive, and a whole lot easier to implement.

@scabug
Copy link
Author

scabug commented Jul 3, 2012

@paulp said:
I think it would be more useful as Seq[Function0[T]], if admittedly harder to implement. If you want a whole sequence with by-name semantics, you can basically do this now with => Seq[Int], at a small price in syntax for the caller - six chars total. If you want a sequence where the elements are individually by name, you need (() => Int)* with a large syntax price for the caller - every sequence member at every call site must be individually wrapped up in a () => .

@scabug
Copy link
Author

scabug commented Jul 3, 2012

@dcsobral said:
How about {{{SeqFunction0[T] <: Seq[T]}}}, that stores a {{{Seq[Function0[T]]}}} internally but calls {{apply()}} whenever an element is applied for? Some code, to make it clearer:

class SeqFunction0[T](xs: Seq[T]) extends Seq[T] with EverythingThatItEntails[T, SeqFunction0[T]] {
  override def apply(n: Int): T = xs(n).apply()
  // any other required overrides
}

I think that most closely mirrors the behavior I would expect:

  def f(xs: => Int*): Int = {
    for (i <- xs.indices) {
      val current = xs(i) // so it won't evaluate twice below
      if (xs(i) % 2 == 1) return xs(i)
    }
    0
  }

  println(f(0, {println("Printed once!"); 2}, 4, 1, ???)) // prints "Printed once!" then "1"

Note: the braces are playing hell with formatting in the first line. I had to opt between no formatting and extraneous { and }, and I opted for the latter.

@scabug
Copy link
Author

scabug commented Jul 3, 2012

@paulp said:
Yours is the same as mine - I wasn't suggesting it actually be a sequence of Function0s, any more than a current => parameter is a Function0. We're using Function0[Seq[T]] vs. Seq[Function0[T]] to identify where the implicit apply takes place; in all cases there is an implicit apply.

@scabug
Copy link
Author

scabug commented Jul 17, 2015

@SethTisue
Copy link
Member

@som-snytt
Copy link

I don't know what it means, but rejected and withdrawn SIPs are accessible as PRs. scala/improvement-proposals#23

Not Found

The requested URL was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

@som-snytt
Copy link

"Backports welcome."

@som-snytt som-snytt closed this as not planned Won't fix, can't repro, duplicate, stale Feb 21, 2024
@SethTisue SethTisue removed this from the Backlog milestone Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants