Skip to content

fix definition of TRY in the language specification#4257

Merged
adriaanm merged 1 commit intoscala:2.11.xfrom
193s:2.11.x
Jan 30, 2015
Merged

fix definition of TRY in the language specification#4257
adriaanm merged 1 commit intoscala:2.11.xfrom
193s:2.11.x

Conversation

@193s
Copy link
Contributor

@193s 193s commented Jan 17, 2015

No description provided.

@scala-jenkins scala-jenkins added this to the 2.11.6 milestone Jan 17, 2015
@Ichoran
Copy link
Contributor

Ichoran commented Jan 17, 2015

Thanks for the patch, but it isn't actually enough. Catch can also take a PartialFunction:

scala> def pf: PartialFunction[Throwable, Unit] = { case e: Exception => println("Caught") }
pf: PartialFunction[Throwable,Unit]

scala> try throw new Exception catch pf
Caught

Any chance you can fix that also?

@som-snytt
Copy link
Contributor

@Ichoran Good catch!

How about:

scala> try ??? catch new { def isDefinedAt(x: Any) = true ; def apply(t: Throwable) = println(t.getMessage) }
warning: there was one feature warning; re-run with -feature for details
an implementation is missing

Syntactically, it takes cases or Expr:

scala> try ??? catch 22
<console>:8: error: value isDefinedAt is not a member of Int
              try ??? catch 22
                            ^

@som-snytt
Copy link
Contributor

Obligatory use case:

scala> implicit class depthFinder(i: Int) {
     | def isDefinedAt(x: Any) = true
     | def apply(t: Throwable) = if (t.getStackTrace.length <= i) throw t
     | }
defined class depthFinder

scala> try ??? catch 22

scala> try ??? catch 100
scala.NotImplementedError: an implementation is missing
  at scala.Predef$.$qmark$qmark$qmark(Predef.scala:225)
  at .liftedTree1$1(<console>:10)
  ... 33 elided

@193s
Copy link
Contributor Author

193s commented Jan 18, 2015

is ('{' Block '}' | Expr) === Expr ?

@som-snytt
Copy link
Contributor

@193s for the try, no, as witnessed by:

scala> val pf: PartialFunction[Any, Int] = { case _ => 42 }
pf: PartialFunction[Any,Int] = <function1>

scala> val pf: PartialFunction[Any, Int] = try { case _ => 42 }
<console>:1: error: '}' expected but 'case' found.
       val pf: PartialFunction[Any, Int] = try { case _ => 42 }
                                                 ^

That's probably an unnecessary limitation, since it only needs to look ahead one token to see if cases or a block is coming. Compare:

scala> try ((s: String) => s.length)
<console>:8: warning: A try without a catch or finally is equivalent to putting its body in a block; no exceptions are handled.
              try ((s: String) => s.length)
              ^
res1: String => Int = <function1>

In fact, it does the look-ahead for catches, which is why it works.

@193s
Copy link
Contributor Author

193s commented Jan 18, 2015

@som-snytt Thanks!

@adriaanm
Copy link
Contributor

Thanks, @193s!

adriaanm added a commit that referenced this pull request Jan 30, 2015
fix definition of TRY in the language specification
@adriaanm adriaanm merged commit c756d25 into scala:2.11.x Jan 30, 2015
@som-snytt
Copy link
Contributor

This PR changes it to Expr on 2.12. There was a related parsing bug SI-5887. One could argue about the generalization to try { case _ => } as shown above, but try (1, 2, ???) is more of a real use case.

#4334

It looks like 2.11 wasn't merged forward yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants