# Idea: throw try away and unify match-catch

**URL:** <https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698>\
**Category:** Language Design\
**Created:** [September 13, 2019, 10:23pm UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698 "2019-09-13T22:23:52Z")\
**Posts on this page:** 17\
**Page:** 2

<div class="post-metadata">

**Author:** ![AndreVanDelft](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/andrevandelft/32/295_2.png) [@AndreVanDelft](https://contributors.scala-lang.org/u/AndreVanDelft)\
**Post date:** [September 16, 2019, 11:38pm UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/21 "2019-09-16T23:38:56Z")

</div>

> [@morgen-peschke](#):
>
> Inverting the example is pretty trivial

Yes it is, but it is still not equivalent to the `match-with-catch` construct that I propose.  
It is not trivial to mimic `match-with-catch` with current Scala constructs. Yet the byte code generation would be easy.

> It’d be problematic because …

I don’t understand what you mean.  
Could you illustrate this, with traditional code and with the `match-with-catch` alternative?

---

<div class="post-metadata">

**Author:** ![jducoeur](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jducoeur/32/32_2.png) [@jducoeur](https://contributors.scala-lang.org/u/jducoeur)\
**Post date:** [September 16, 2019, 11:44pm UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/22 "2019-09-16T23:44:26Z")

</div>

> [@AndreVanDelft](#):
>
> I don’t see how that would problematic in practice.

Honestly, I find that _very_ unintuitive and unclear. I’d probably veto it if I ever saw it in a code review, whether it was legal syntax or not…

---

<div class="post-metadata">

**Author:** ![som-snytt](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/som-snytt/32/595_2.png) [@som-snytt](https://contributors.scala-lang.org/u/som-snytt)\
**Post date:** [September 17, 2019, 12:01am UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/23 "2019-09-17T00:01:40Z")

</div>

I’ll cop to using “brainwash” in an ungenerous way.

For me, the term was hot-button because of [Patty Hearst](https://en.wikipedia.org/wiki/Patty_Hearst#Brainwashing_claims) but today I understand the term as such to be discredited, so in fact I used it jocularly, though not unseriously.

I did intend to say that we can’t think rationally about the try construct. An expression that throws is simply too special in the Java ecosystem. (More later.)

I did not intend to compare Scala syntax to various forms of coercion that are applied cruelly and unusually in this world.

Since I am liable to express myself in terms that might reasonably be construed to be over-the-top, I’m willing to justify this over-the-top term in this context.

I think there is a distinction between language policing which says “You can’t say that” and a conversation which says “You have to justify your words, or at least use more words.”

In the spirit of using more words, my Java friend insists that Javadoc list all throwables, and also that throws clauses list unchecked throwables. This is so that the IDE will always tell you what throws where. I prefer the Scala solution, that everything throws anything, and you catch where it is meaningful to do so. I just remembered the phrase, “catch as catch can”.

So I do think there is a “throws” culture who buy into throwing as a way of passing control up the stack, as opposed to a functional or value culture who would prefer to pass a value up the stack, perhaps an Either.

I just experienced this distinction recently in a Java project where I had introduced a `Try`. I added some API and realized I could now just return a `Try`. I felt like such a fool.

So I do insist that our ability to conceptualize is limited by previous thinking. Maybe someone could remind me of the term for that, something more precise than “brainwashing”. It seems to me that Althusser had a word for it, but after so many years, the word escapes me.

Edit: sorry to derail the conversation, and also thanks for the responses attempting to parse what I might have said, etc.

---

<div class="post-metadata">

**Author:** ![morgen-peschke](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/morgen-peschke/32/2528_2.png) [@morgen-peschke](https://contributors.scala-lang.org/u/morgen-peschke)\
**Post date:** [September 17, 2019, 4:38am UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/24 "2019-09-17T04:38:21Z")

</div>

> [@som-snytt](#):
>
> So I do think there is a “throws” culture who buy into throwing as a way of passing control up the stack, as opposed to a functional or value culture who would prefer to pass a value up the stack, perhaps an Either.

I think this is called “Exceptions as flow control” (?). For what it’s worth, I don’t see `try` as part of this as much as a shim for interacting with Java libraries ¯\_(ツ)\_/¯

> [@som-snytt](#):
>
> So I do insist that our ability to conceptualize is limited by previous thinking. Maybe someone could remind me of the term for that, something more precise than “brainwashing”.

Possibly, “stuck in the `throw` paradigm”? I also sort of like, “hanging onto their Java accent”

---

<div class="post-metadata">

**Author:** ![morgen-peschke](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/morgen-peschke/32/2528_2.png) [@morgen-peschke](https://contributors.scala-lang.org/u/morgen-peschke)\
**Post date:** [September 17, 2019, 4:55am UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/25 "2019-09-17T04:55:51Z")

</div>

> [@AndreVanDelft](#):
>
> Yes it is, but it is still not equivalent to the `match-with-catch` construct that I propose.  
> It is not trivial to mimic `match-with-catch` with current Scala constructs. Yet the byte code generation would be easy.

Took a couple hours to sink in, and rereading it a few times, but I _think_ I understand what you’re trying to say. I still disagree, but at least I think I grok it a bit better.

The inverted version I posted has exactly these semantics:

> [@AndreVanDelft](#):
>
> `e3` would handle exceptions occurring in `e1` , but **not** those in `e2` .  
> This way
> 
> - `e2` handles the successful results of `e1`
> - `e3` handles failures of `e1`

This becomes easier to see when you inline the blocks:

```auto
try e1 catch {
  case failedResult => e3
} match {
  case successResult => e2
}

```

Incidentally, this effectively `Try.fold`, but blessed into the language.

Introducing new syntax would help differentiate this from the existing `try`, but an automatic rewrite _would_ cause the problems I mentioned. Specifically, it’s this bit that causes the trouble:

> [@AndreVanDelft](#):
>
> For a future `f` as a recipient of `yield match` etc, an implicit conversion should be in scope that turns `f` into a `Yield_match_catch_handler` with methods `yield_match` , `yield_catch` and `yield_match_with_catch` calling the future’s `onSuccess` and `onFailure` methods.

You’d create a situation where an error handling construct couldn’t be scoped to handle errors which trigger before the `Future` takes over, because it would automatically lift the block into the context of the returned `Future`. This only works if there’s no chance exceptions can be thrown outside of the `Future`, which is (unfortunately) not the case.

I’m also really unsure of the wisdom of overloading the meaning of `yield`. Embedding this in a `for` comprehension would look a bit off:

```auto
for {
  a <- f1
  b <- f2
} yield a + b yield match {
  case r => 
} with catch {
  case e => 
}

```

---

<div class="post-metadata">

**Author:** ![AndreVanDelft](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/andrevandelft/32/295_2.png) [@AndreVanDelft](https://contributors.scala-lang.org/u/AndreVanDelft)\
**Post date:** [September 17, 2019, 8:35am UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/26 "2019-09-17T08:35:03Z")

</div>

> [@morgen-peschke](#):
>
> The inverted version I posted has exactly these semantics:

That is not true. A concrete example:

```scala
{throw new Exception} 
match {case _ => print("Success"}
with 
catch {case _ => print("Failure"}

```

prints: “ **Success** ”

Edit: Oops. that should be **“Failure”**!

Your interpretation, with an extra pair of braces to get it compiled:

```scala
{ try {throw new Exception} catch {
  case failedResult => print("Failure")
} } match {
  case successResult => print("Success")
}

```

prints: “ **FailureSuccess** ”.

With respect to the version for futures and possibly similar constructs:

```scala
f yield match e1 
   with catch e2

```

would behave exactly the same as

```scala
f.onSuccess{e1}
f.onFailure{e2}

```

unless I am overlooking something.  
AFAICS there is no strange behaviour when an `Error` is thrown.

Edit: I see now that `onSuccess` and `onFailure` are deprecated. Instead of the fragment just above, it should be

```scala
f. foreach{e1}
f.failed.foreach{e2}

```

---

<div class="post-metadata">

**Author:** ![AndreVanDelft](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/andrevandelft/32/295_2.png) [@AndreVanDelft](https://contributors.scala-lang.org/u/AndreVanDelft)\
**Post date:** [September 17, 2019, 8:36am UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/27 "2019-09-17T08:36:58Z")

</div>

> [@morgen-peschke](#):
>
> I’m also really unsure of the wisdom of overloading the meaning of `yield` . Embedding this in a `for` comprehension would look a bit off:

Your counter example would typically not compile, because the second `yield` would require that the preceding expression is of a type `Yield_match_catch_handler`.

---

<div class="post-metadata">

**Author:** ![sjrd](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sjrd/32/24_2.png) [@sjrd](https://contributors.scala-lang.org/u/sjrd)\
**Post date:** [September 17, 2019, 3:27pm UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/28 "2019-09-17T15:27:02Z")

</div>

@AndreVanDelft Your `match .. catch` does not seem to solve a _common_ problem to me. There should be a very high bar to put something in the standard library, but an even higher bar to put it _in the language_.

If this is a common problem _in your codebase_, you can implement it in user space:

```scala
object Foo {
  implicit def MatchCatchOps[A](self: => A): MatchCatchOps[A] = new MatchCatchOps(() => self)
  class MatchCatchOps[A](val self: () => A) extends AnyVal {
    def matchC[B](pf: PartialFunction[A, B]): MatchC[A, B] =
      new MatchC(self, pf)
  }
  class MatchC[A, B](self: () => A, pf: PartialFunction[A, B]) {
    def catchM(pfCatch: PartialFunction[Throwable, B]): B = {
      val e = try Right(self()) catch { case th: Throwable => Left(pfCatch(th)) };
      e.map(pf).fold(identity(_), identity(_))
    }
  }
}

```

then:

```scala
scala> import Foo._
import Foo._

scala> { 5: Int } matchC { case 5 => throw new IllegalArgumentException; case _ => "Success" } catchM { case _ => "Failure" }
java.lang.IllegalArgumentException
  at $anonfun$1.applyOrElse(<console>:16)
  at $anonfun$1.applyOrElse(<console>:16)
  at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:38)
  at scala.util.Either.map(Either.scala:353)
  at Foo$MatchC.catchM(<console>:11)
  ... 28 elided

scala> { 6: Int } matchC { case 5 => throw new IllegalArgumentException; case _ => "Success" } catchM { case _ => "Failure" }
res5: String = Success

scala> { (throw new Exception): Int } matchC { case _ => "Success" } catchM { case _ => "Failure" }
res6: String = Failure

```

---

<div class="post-metadata">

**Author:** ![morgen-peschke](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/morgen-peschke/32/2528_2.png) [@morgen-peschke](https://contributors.scala-lang.org/u/morgen-peschke)\
**Post date:** [September 17, 2019, 3:57pm UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/29 "2019-09-17T15:57:01Z")

</div>

> [@AndreVanDelft](#):
>
> Your counter example would typically not compile, because the second `yield` would require that the preceding expression is of a type `Yield_match_catch_handler` .

If this is to be universally available syntax, it certainly should.

If it isn’t, then there’s no real basis to put it into the standard library - just use `Future.transform`

---

<div class="post-metadata">

**Author:** ![morgen-peschke](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/morgen-peschke/32/2528_2.png) [@morgen-peschke](https://contributors.scala-lang.org/u/morgen-peschke)\
**Post date:** [September 17, 2019, 4:04pm UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/30 "2019-09-17T16:04:01Z")

</div>

> [@AndreVanDelft](#):
>
> > The inverted version I posted has exactly these semantics:
> 
> That is not true. A concrete example:
> 
> ```auto
> {throw new Exception} 
> match {case _ => print("Success"}
> with 
> catch {case _ => print("Failure"}
> 
> ```
> 
> prints: “ **Success** ”

While that’s a fair point, I think that example is backwards, as throwing an `Exception` resulting in printing `"Success"` would be deeply counterintuitive.

To get exactly the semantics you seem to want, you would need to use `Try`:

```auto
Try(throw new Exception)
  .fold(_ => print("Success"), _ => print("Failure"))

```

With the automatic lift, what would you expect your proposed syntax to do in this case:

```auto
{ 
  throw new IllegalStateException("Seems like this would be uncatchable, due to the transformation to Future.yieldMatchWithCatch")
  Future.successful("Foo")
} yield match { case success => print(s"Returned $success (inside Future)") }
  with
  catch { case failure => print(s"Return $failure (inside Future)" }

```

---

<div class="post-metadata">

**Author:** ![AndreVanDelft](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/andrevandelft/32/295_2.png) [@AndreVanDelft](https://contributors.scala-lang.org/u/AndreVanDelft)\
**Post date:** [September 17, 2019, 4:43pm UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/31 "2019-09-17T16:43:22Z")

</div>

> [@morgen-peschke](#):
>
> > prints: “ **Success** ”
> 
> While that’s a fair point, I think that example is backwards, as throwing an `Exception` resulting in printing `"Success"` would be deeply counterintuitive.

Yes that would be so…I made an error; I should have written:

> prints: **“Failure”**

I edited that in my previous post.

---

<div class="post-metadata">

**Author:** ![AndreVanDelft](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/andrevandelft/32/295_2.png) [@AndreVanDelft](https://contributors.scala-lang.org/u/AndreVanDelft)\
**Post date:** [September 17, 2019, 4:53pm UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/32 "2019-09-17T16:53:07Z")

</div>

> [@morgen-peschke](#):
>
> With the automatic lift, what would you expect your proposed syntax to do in this case:
> 
> ```auto
> { 
> throw new IllegalStateException("Seems like this would be uncatchable, due to the transformation to Future.yieldMatchWithCatch")
> Future.successful("Foo")
> } match { case success => print(s"Returned $success (inside Future)") }
> with
> catch { case failure => print(s"Return $failure (inside Future)" }
> 
> ```

Not sure whether I should answer this after discovering my “Success”/“Failure” mistake.

I assume you meant `yield match` rather than `match`, in your last code fragment. Then the `IllegalStateException` would indeed not be caught.

---

<div class="post-metadata">

**Author:** ![morgen-peschke](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/morgen-peschke/32/2528_2.png) [@morgen-peschke](https://contributors.scala-lang.org/u/morgen-peschke)\
**Post date:** [September 17, 2019, 4:58pm UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/33 "2019-09-17T16:58:11Z")

</div>

Yep, I meant `yield match`, good catch.

It seems like not catching the `IllegalStateException` would be a big problem.

Currently, this can be handled by wrapping the main block in a `try`, and handling the error inside the `Future` elsewhere, which seems much easier to follow:

```auto
val future = try {
  throw new IllegalStateException
  Future.failed(new Exception)
} catch {
  case e: IllegalStateException => print(s"Failure to launch: $e")
}

future.transform(
  r => print(s"Success: $r"),
  e => print(s"Failed: $e")
)

```

The proposed syntax would require nesting inside an additional `try`, which would be a bit of a mess:

```auto
try { 
  {
    throw new IllegalStateException
    Future.successful("Foo")
  } yield match { case success => print(s"Returned $success (inside Future)") }
    with
    catch { case failure => print(s"Return $failure (inside Future)" }
} catch {
  case e: IllegalStateException => print(s"Failure to launch: $e (outside Future)"}
}

```

---

<div class="post-metadata">

**Author:** ![AndreVanDelft](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/andrevandelft/32/295_2.png) [@AndreVanDelft](https://contributors.scala-lang.org/u/AndreVanDelft)\
**Post date:** [September 17, 2019, 5:41pm UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/34 "2019-09-17T17:41:33Z")

</div>

> [@sjrd](#):
>
> If this is a common problem _in your codebase_ , you can implement it in user space:

Thank you; that is a neat piece of code!  
It makes me almost wonder why there is a `match` construct.

For the `yield` variants a similar approach would apply.

---

<div class="post-metadata">

**Author:** ![godenji](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/godenji/32/1068_2.png) [@godenji](https://contributors.scala-lang.org/u/godenji)\
**Post date:** [September 20, 2019, 2:51pm UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/35 "2019-09-20T14:51:37Z")

</div>

> I don’t really think this needs improving!

```auto
try readList() match
  case Nil => 1
  case x :: xs => 2
catch
  case IOException => 3
  case Error => 4

```

and yet it does need improving; let’s try 🙂

```auto
try readList() match
  Nil => 1
  x :: xs => 2
catch
  IOException => 3
  Error => 4

```

With whitespace significant syntax there should be no need for `case` in `match` clauses (or perhaps anywhere in the language outside of `case class` definitions).

This would align Scala more closely syntactically to the ML family of languages, which are IMO rather pleasing to the eye, both terse and readable.

---

<div class="post-metadata">

**Author:** ![sighoya](https://contributors.scala-lang.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@sighoya](https://contributors.scala-lang.org/u/sighoya)\
**Post date:** [September 20, 2019, 4:02pm UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/36 "2019-09-20T16:02:58Z")

</div>

> With whitespace significant syntax there should be no need for `case` in `match` clauses (or perhaps anywhere in the language outside of `case class` definitions).

Will dotty really become whitespace sensitive?

---

<div class="post-metadata">

**Author:** ![jducoeur](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jducoeur/32/32_2.png) [@jducoeur](https://contributors.scala-lang.org/u/jducoeur)\
**Post date:** [September 20, 2019, 9:12pm UTC](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698/37 "2019-09-20T21:12:20Z")

</div>

> [@sighoya](#):
>
> Will dotty really become whitespace sensitive?

Remember that Dotty is basically the experimental testbed. At the moment, it is whitespace sensitive under certain circumstances, but that’s controversial – we’ll see whether that makes it into Scala 3.

[Previous page](https://contributors.scala-lang.org/t/idea-throw-try-away-and-unify-match-catch/3698.md?page=1)
