# Pre-RFC: Catching Functions

**URL:** <https://internals.rust-lang.org/t/pre-rfc-catching-functions/6505>\
**Category:** language design\
**Created:** [January 8, 2018, 11:33pm UTC](https://internals.rust-lang.org/t/pre-rfc-catching-functions/6505 "2018-01-08T23:33:02Z")\
**Posts on this page:** 1\
**Showing post:** 145

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [January 12, 2018, 4:23am UTC](https://internals.rust-lang.org/t/pre-rfc-catching-functions/6505/145 "2018-01-12T04:23:31Z")

</div>

> [@Centril](#):
>
> What are the main reservations against allowing implicit Try::from\_ok(From::from(expr)) wrapping for return expr for types that are Try?

My biggest complaint with it is that it means `return Some(4)` and `return None` do drastically-different things in a function that's `-> Option<Option<i32>>`.

See [rfcs/text/0000-ok-wrapping.md at ok-wrapping · scottmcm/rfcs · GitHub](https://github.com/scottmcm/rfcs/blob/ok-wrapping/text/0000-ok-wrapping.md#make-this-a-general-coercion) for more.

(And there definitely shouldn't be a `From::from` in there, since that keeps things like `return 4;` from working normally.)

> [@mark-i-m](#):
>
> I think closures can just use a raw catch block-expression, right?
> 
> ```rust
> |x| catch { v.get(x)?.to_string() }
> 
> ```

But is that function-level catch or a catch expression? It matters if there's a `return` in the closure.

> [@mark-i-m](#):
>
> It does solve function-level catch being magic though…

It needs to be a bit magic, though, so code is more similar between fallible and infallible functions.

> [@KasMA1990](#):
>
> the net win in lines of code to deal with its quite small

I think counting characters is absolutely the wrong measurement here. The big advantage I want out of this is that it becomes normal to _always_ type the `?` after something you know can fail as the first pass, allowing the first pass at the method to be written just like it was infallible.

> [@nikomatsakis](#):
>
> It may be worth revisiting the idea of try { .. } as the syntax for a catch block (and maybe that leads us somewhere at the fn signature level?).

Reminds me of this paper...

[https://www.microsoft.com/en-us/research/publication/exceptional-syntax/](https://www.microsoft.com/en-us/research/publication/exceptional-syntax/)

> [@withoutboats](#):
>
> I think its harder to spot

I think this is partially due to a lack of keyword highlighting. Changing a letter to get the highlighting helps it stand out.

```rust
fn foo<F: Fn(&File) -> bool>(&mut self, f: F) -> Result<i32, io::Error> {
fn foo<F: Fn(&File) -> bool>(&mut self, f: F) -> match Result<i32, io::Error> {
fn foo<F: Fn(&File) -> bool>(&mut self, f: F) -> i32 match io::Error {

```

And one thing I like about having the type there is that it allows aliases, as are common today:

```rust
fn foo<F: Fn(&File) -> bool>(&mut self, f: F) -> match io::Result<i32> {
                                                // no `Error` anywhere

```

(To be super-explicit just in case: I'm definitely not suggesting `match` as the keyword 😁)

---

_[View the full topic](https://internals.rust-lang.org/t/pre-rfc-catching-functions/6505)._
