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
Allow Implicit Modifier on Parameters to Anonymous Methods #1492
Comments
Imported From: https://issues.scala-lang.org/browse/SI-1492?orig=1 |
Nathan Bronson (nbronson) said: for (implicit txn <- atomic) {
// body
} |
@jesnor said: (implicit i : Int) => {...}
// Or with type inference: implicit i => {...} should be equivalent to: (i : Int) => { implicit val _ = i; ... }
// Or with type inference: i => { implicit val _ = i; ... } If so, I think it's a valuable addition to the language. |
@ijuma said: "A named parameter of an anonymous function may be optionally precdeded" |
@odersky said: |
@jorgeortiz85 said: If it's not asking too much, can we also have: for (implicit a <- ...) {
...
} |
@paulp said: Example: I have a method mainMethod which I use thusly. def mainMethod(f: MethodVisitor => Unit): Unit = { ... }
// from elsewhere
cw mainMethod { mw =>
implicit val _mw = mw
// the methods being called here by way of implicit take an implicit mw
("java/lang/System" getStatic "out")
("java/io/PrintStream" invokeVirtual "println")("Hello world!")
("java/lang/System" invokeStatic "setProperty")("propfoo", "valuebar")
} So naturally I'd be pleased to be able to write: cw mainMethod { implicit mw => ... But that does not parse. The only way I can get it to parse are when I explicitly type it prior to the parameter being used. I failed to find any way to annotate the type of the parameter or to use an anonymous parameter. So here is how it looks if I accept the limitations: val body: MethodVisitor => Unit = implicit mw => {
("java/lang/System" getStatic "out")
("java/io/PrintStream" invokeVirtual "println")("Hello world!")
("java/lang/System" invokeStatic "setProperty")("propfoo", "valuebar")
}
cw mainMethod body But this is far inferior to the original version. |
@odersky said: |
curious, why only single-parameter? this came up today on Gitter SLS 6.23 says "A named parameter of an anonymous function may be optionally preceded by an implicit modifier.", so the single-parameter restriction isn't in the spec language |
oops, misclick |
There are other rather odd restrictions associated with this, as well, such as the fact that the I suspect this is all because it's a relatively obscure feature which was originally added somewhat rapidly in response to my ill-considered suggestion (which itself came about because I was trying to thread STM transaction identity in a syntactically-nice way). Realistically, I think this bit of syntax could use some attention in Scala 3 (probably 3.1 at this point), removing some of these gotchas and integrating it more cleanly into the language. This could all be done in a fully compatible way, since the syntax would be a strict superset ( If I were truly motivated, I would draft a SIP. But this hasn't really crossed my threshold of motivation in the thirteen-ish years since this was added, which perhaps is the real answer to the question of "why": no one has felt strongly enough to do anything about it! |
In Scala 3, the
|
Specifically, I would like to be able to do the following:
As of 2.7.1, this triggers a parse error. As far as I can tell, allowing this should not affect soundness and will not introduce any ambiguities in the grammar.
This capability is useful for situations like my implicit STM which uses function values which take implicit parameters. Without this functionality, every function used in such a fashion must be declared as a full, named function. This is ugly and too heavily drains the imagination (seeking unique identifiers).
The text was updated successfully, but these errors were encountered: