Skip to content

by-name parameter does not work with function literal syntax #9313

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

Open
scabug opened this issue May 14, 2015 · 9 comments
Open

by-name parameter does not work with function literal syntax #9313

scabug opened this issue May 14, 2015 · 9 comments
Milestone

Comments

@scabug
Copy link

scabug commented May 14, 2015

scala> val v = (headVal: Int, currentResultStream: => Int) => head
:1: error: identifier expected but '=>' found.
val v = (headVal: Int, currentResultStream: => Int) => head
^

@scabug
Copy link
Author

scabug commented May 14, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9313?orig=1
Reporter: Saurabh Wadhawan (saurabh18e)
Affected Versions: 2.10.4

@scabug
Copy link
Author

scabug commented May 14, 2015

Saurabh Wadhawan (saurabh18e) said:
by-name parameter syntax should work with function literal syntax as well

@scabug
Copy link
Author

scabug commented May 15, 2015

@retronym said:
This is supported:

scala> val f: (Boolean, => Int) => Int = (b, i) => if (b) i else 0
f: (Boolean, => Int) => Int = <function2>

scala> f(false, ???)
res0: Int = 0

scala> f(true, ???)
scala.NotImplementedError: an implementation is missing
  at scala.Predef$.$qmark$qmark$qmark(Predef.scala:225)

@scabug
Copy link
Author

scabug commented May 15, 2015

Saurabh Wadhawan (saurabh18e) said:
val f1 = (i: Int) => 1
above works

but below doesn't work
val f2 = (i: => Int) => 1

the by-name parameter symbol should be specified in the definition on RHS rather than in the type on LHS

@scabug
Copy link
Author

scabug commented May 15, 2015

Saurabh Wadhawan (saurabh18e) said:
scala> val f2 = (i: => Int) => 1
:1: error: identifier expected but '=>' found.
val f2 = (i: => Int) => 1
^

scala> val f2: (=> Int) => Int = (i) => 1
f2: (=> Int) => Int =

@Jasper-M
Copy link

Jasper-M commented Mar 29, 2018

The syntax specification says:

Binding  ::=  (id | ‘_’) [‘:’ Type]

Is there a reason I'm not seeing why it can't be this?

Binding  ::=  (id | ‘_’) [‘:’ ParamType]

ParamType is a Type that can also be by-name (=> Type) or varargs (Type*). At first glance that would make the syntax more regular, but perhaps I'm missing something.

@Ichoran
Copy link

Ichoran commented Mar 7, 2019

Just a note that this is still an issue; someone ran into this today and asked about it on gitter.

@SethTisue
Copy link
Member

SethTisue commented Aug 17, 2021

not fixed in Scala 3. this came up again on Gitter today. https://gitter.im/scala/scala?at=611c09487bc44d0a472893e0

scala> val x: (=> Int) => Int = (x: =>Int) => x                                                                         
1 |val x: (=> Int) => Int = (x: =>Int) => x
  |                             ^^
  |                             an identifier expected, but '=>' found

scala> val x: (=> Int) => Int = (x: (=>Int)) => x                                                                       
1 |val x: (=> Int) => Int = (x: (=>Int)) => x
  |                              ^^^^^
  |                           By-name parameter type => Int not allowed here.

scala> val x: (=> Int) => Int = (x: Int) => x                                                                           
1 |val x: (=> Int) => Int = (x: Int) => x
  |                         ^^^^^^^^^^^^^
  |                         Found:    Int => Int
  |                         Required: (=> Int) => Int

scala> val x: (=> Int) => Int = x => x
val x: (=> Int) => Int = Lambda$1335/1897050650@3a17acd4

that's Scala 3, but Scala 2.13.6 gives similar errors.

@bbarker
Copy link

bbarker commented Aug 19, 2021

Cross-referencing proposal on Discourse: Make by-name parameters just sugar syntax rather than actual types

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

5 participants