-
-
Save jmhodges/9282988 to your computer and use it in GitHub Desktop.
object Welp { | |
def foobar(a: Int, b: Int, c:Int) { throw new IllegalArgumentException } | |
def main(args: Array[String]) { | |
List(3) map { | |
try { | |
foobar(1, 2, _) | |
} catch { | |
case e: Throwable => { // This set of braces is optional. Problem remains when removed. | |
println("won't be printed") | |
throw e | |
}} | |
} | |
} | |
} |
#!/bin/bash | |
scalac Welp.scala && scala -classpath . Welp |
and here's what should happen:
Don't you mean, "here's what the programmer intended to happen"?
Here is an similar example using a side-effecting identity rather than try/catch
:
scala> def sideEffect[T](t: T) = { println("!"); t }
sideEffect: [T](t: T)T
scala> def foo(a: Int) = a
foo: (a: Int)Int
scala> val f = sideEffect(foo(_))
!
f: Int => Int = <function1>
scala> f(0)
res0: Int = 0
The placeholder syntax for anonymous functions is specified to create the function with the smallest possible scope.
Don't you mean, "here's what the programmer intended to happen"?
Right, sorry, that's what I meant. Seems this behavior conforms to the spec, even if it's not intuitive:
An expression e of syntactic category Expr binds an underscore section u, if the following two conditions hold: (1) e properly contains u, and (2) there is no other expression of syntactic category Expr which is properly contained in e and which itself properly contains u.
Since:
Expr ::= (Bindings | [‘implicit’] id | ‘_’) ‘=>’ Expr
| Expr1
Expr1 ::= ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[semi] else Expr]
| ‘while’ ‘(’ Expr ‘)’ {nl} Expr
| ‘try’ ‘{’ Block ‘}’ [‘catch’ ‘{’ CaseClauses ‘}’]
[‘finally’ Expr]
...
| SimpleExpr1 ArgumentExprs ‘=’ Expr
...
SimpleExpr1 := ...
| `_'
...
Now I just wonder what properly contains
means in plain English.
@VladUreche I think it just means "below it in the parse tree", which may be plain spokenness for something like "on the right-hand side of a production." After I read that in the spec, I was no longer misled by stuff about parens and contexts in SO answers to this issue. (Probably I found other things to be misled by.)
@retronym "side-effecting identity" is one of those phrases that make me glad to have found FP, but to me, the try-catch version is simpler and more devious somehow. If it's OK, I'll submit that version to the puzzlers. For all I know, they are already puzzling over it. (In the real world, identity is always side-effecting, if not self-effacing.)
I hope this does justice to the suffering of all involved.
/** Top-level component for welp.com domain.
* Therefore, must never throw an exception.
*/
object Welp {
/** Start serving welp pages. */
def f(a: Int, b: Int, c: String) { throw new IllegalArgumentException }
def fail(e: Throwable) = throw new RuntimeException(s"""${"*" * 30}
|Dan, I hope you see this in the log file,
|because the server just crashed.
|
|Dan, wake up!
|${e.getMessage}
|${"*" * 30}
|""".stripMargin)
def main(args: Array[String]): Unit = args map {
try f(1, 2, _)
catch {
case e: Throwable => Console.err println "Failing..." ; fail(e)
}
}
}
with the joke answer (because it's multiple choice)
Prints:
Failing...
and wakes Dan at 3 a.m. when someone notices the server is down.
Feel free to comment on PR; though the puzzler guys are good at polishing.
The title is "Catch me if you can." Of course.
BTW, the puzzler guys are going to cut down some trees and print puzzlers on them. If you want your name associated with said puzzles, or if you wish to deny all association, you should tell them. The puzzler links to the ML discussion, as a rule.
@adriaanm Oh, yeah, my commenting out the throw actually made it not compile. I've put it back.
Now I just wonder what
properly contains
means in plain English.
That isn't plain English, but "math English". I think "properly" there is analogous to proper subset, even though we're not talking about sets: an expression e1 is properly contained into e2 if e1 is contained in e2 and e1 is different from e2. But I guess clarifying this in the spec could be a good idea.
@adriaanm: I couldn't reproduce it either, but here's a version that reproduces the problem:
and here's what should happen: