question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow by-name repeated parameters

See original GitHub issue

Motivation

Scala so far does not allow by-name repeated parameters. But I can’t see a good reason why this combination should be disallowed. Also, the combination is necessary to allow vararg parameters that are passed as expressions to inline methods (instead of being lifted out).

Syntax

The syntax for ParamType becomes

  ParamType         ::=  [`=>'] ParamValueType
  ParamValueType    ::=  Type [`*']              

The syntax implies that a type such as => T*, which is both by-name and repeated is interpreted as => (T*), that is, as a by-name type of a repeated type.

Translation Rules

If a parameter has a by-name repeated type => T* it matches an arbitrary number of actual arguments of type T. As usual for by-name parameters, the arguments are not evaluated at the point of call. Instead, all arguments are evaluated each time the parameter is referenced in the called method.

The same holds for an vararg argument of the form e: _*. The argument expression e is evaluated each time the parameter is referenced in the called method.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
nafgcommented, Dec 8, 2016

It’s more general sure, but as Runar says, liberties constrain and constraints liberate. Or as the Talmud says, whoever increases, decreases. Specifically, as Martin has said, by taking the more general interpretation the implementation is forced to be much less performant. Also (he said) it would a much more complicated implementation. How would you implement such a thing? In fact how would it even work? Given def m(xs: =>Int*) { // what is the type of xs? val ys = xs.to[X] // where X is List, Seq, whatever. What is the element type? val z = xs.head // what does this desugar to? xs.filter(somePredicate).foreach(println) // what are the semantics? } I guess you could either (1) make xs have some special collection type, e.g. LazyList, or (2) make it be some sort of Seq[=>Int], with new rules.

On second thought, if we had an actual LazyList as has been discussed (unlike Stream where the head is not truly lazy), one could suggest that T* becomes a Seq while =>T* becomes a LazyList. But actually, by-name is different than lazy: lazy means evaluated 0 or 1 times, but by-name means evaluated 0 or more times. So you’d need a new collection (ByNameSeq) which is like LazyList but much less useful.

On a different note (OT), it would be interesting to be able to declare parameters to be like lazy rather than regular call-by-name.

On Fri, Aug 19, 2016 at 12:28 PM Sean Vieira notifications@github.com wrote:

I would argue that treating => T* as a Seq[Function0[T]] makes far more sense that Function0[Seq[T]] because it is possible to do everything the latter enables in terms of the former, but not vice-versa.

An example:

def firstSuccessful(actions: => Boolean*) = actions.toStream.map(action => action()).isDefined

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lampepfl/dotty/issues/499#issuecomment-241065990, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGAUMfX67MF6WCEK06IOYqAonsWjZdTks5qhdmigaJpZM4EH-0W .

1reaction
nafgcommented, Apr 24, 2015

Seems to work (in 2.11.6): http://scastie.org/9184

Read more comments on GitHub >

github_iconTop Results From Across the Web

By-name repeated parameters - scala - Stack Overflow
How to pass by-name repeated parameters in Scala? The following code fails to work: scala> def foo(s: (=> String)*) ...
Read more >
SIP-24 - Repeated By Name Parameters - Scala Documentation
The syntax implies that a type such as => T* , which is both by-name and repeated is interpreted as => (T*) ,...
Read more >
Std Lib | Repeated Parameters - Scala Exercises
A repeated parameter must be the last parameter and this will let you add as many extra parameters as needed. Given: def repeatedParameterMethod(x:...
Read more >
Pass-By-Name Parameter Passing
In essence, you can pass in the symbolic "name"; of a variable, which allows it both to be accessed and updated. For example,...
Read more >
How to Use By-Name Parameters in Scala | alvinalexander.com
A by-name parameter is like receiving a def method; its body is evaluated whenever it is used inside the function. Those statements aren't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found