# Using reserved words/syntax (try, ?) as macro names

**URL:** <https://discourse.julialang.org/t/using-reserved-words-syntax-try-as-macro-names/34063>\
**Category:** Internals & Design\
**Created:** [February 1, 2020, 3:19pm UTC](https://discourse.julialang.org/t/using-reserved-words-syntax-try-as-macro-names/34063 "2020-02-01T15:19:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin.squire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin.squire/32/62_2.png) [@kevin.squire](https://discourse.julialang.org/u/kevin.squire)\
**Post date:** [February 1, 2020, 3:19pm UTC](https://discourse.julialang.org/t/using-reserved-words-syntax-try-as-macro-names/34063/1 "2020-02-01T15:19:43Z")

</div>

TL;DR: I’d like to be able to use `@try` or `@?` as macro names for `try-catch`-like syntax in code using `Result` types. Would this be reasonable to allow?

############

I’ve been exploring `ResultTypes.jl` as an alternative to throwing exceptions. I quite like it so far, but I found the code to be somewhat verbose:

```julia
Base.convert(::Type{FooError}, err::BarError) = FooError("Got a BarError: \$(err.msg)")

function foo(x)::Result{Int, FooError}
    result = isbar(x) # result is a Result{Bool, BarError}
    ResultTypes.iserror(result) && return unwrap_error(result)
    is_b = unwrap(result)
    return is_b ? 42 : 13
end

# ResultTypes.jl has `convert` methods for converting the return values
# above into the proper `Result` type.

```

Rust, in particular, uses `Result` (and similar types) exclusively, instead of exceptions. For the syntax above, they used to have a `try!` macro, which would look something like this in Julia:

```julia
function foo(x)::Result{Int, FooError}
    is_b = @try! isbar(x) # expands to the version of foo() above
    return is_b ? 42 : 13
end

```

This is much nicer!

Using `try` makes sense, because it’s playing the role of `try-catch` for `Result` types.

I would prefer `@try` over `@try!` (the `!` in `try!` indicates a macro call in Rust; in Julia, it hints that we’re modifying an argument, but we’re not doing that here). However, `try` is a reserved word in Julia, and can’t be used for macro names.

Rust now actually uses `?` to accomplish the same thing. In Julia, this would look like

```julia
function foo(x)::Result{Int, FooError}
    is_b = isbar(x)? # doesn't actually work in Julia
    return is_b ? 42 : 13
end

```

To somewhat mimic this, I also tried defining `@?` instead of `@try`, but that didn’t work either (`?` is syntax).

My question to the mainline devs: would it be reasonable to allow either of these to be used as macro names?

For anyone, any other ideas/suggestions on how to name or implement this? (See [https://github.com/iamed2/ResultTypes.jl/pull/17](https://github.com/iamed2/ResultTypes.jl/pull/17) for the current pull request.)

Thanks!  
Kevin

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [February 1, 2020, 3:30pm UTC](https://discourse.julialang.org/t/using-reserved-words-syntax-try-as-macro-names/34063/2 "2020-02-01T15:30:28Z")

</div>

It’s already possible to use `@try` etc as macro names:

```julia
julia> macro foo(ex) ex end
@foo (macro with 1 method)

julia> @eval $(Symbol("@try")) = $(Symbol("@foo"))
@foo (macro with 1 method)

julia> @try(1)
1

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [February 1, 2020, 4:15pm UTC](https://discourse.julialang.org/t/using-reserved-words-syntax-try-as-macro-names/34063/3 "2020-02-01T16:15:11Z")

</div>

Note that in Julia 1.3+, you can write the second line a bit nicer without eval as

```julia
const var"@try" = var"@foo"

```
