SimpleMatch.jl

Local case matching made super intuitive
Author jolin-io
Popularity
3 Stars
Updated Last
1 Year Ago
Started In
January 2020

SimpleMatch

Build Status Coverage

Install with

using Pkg
pkg"add SimpleMatch"

and load it

using SimpleMatch

which gives you access to the one and only macro of this package @match.

Usage

With match you can define local dispatch, i.e. you can rewrite

myhelperfunctionname(a::Int) = a + 2
myhelperfunctionname(a::String) = a * "!"
myhelperfunctionname("hi")  # "hi!""

as

@match("hi") do f
  f(a::Int) = a + 2
  f(a::String) = a * "!"
end
# "hi!""

You get full standard dispatch but don't have to care any longer about how to name your intermediate function. Just name it f always. The do-syntax guarantees that this is a local function which cannot interfere at all.

Of course you can also dispatch on values using standard Val. E.g. very useful when working with Base.Expr

expr = :(function dummy end)
a = expr.head
@match(Val(a)) do f
  f(::Val{:function}) = "a function definition"
  f(::Val{:(=)}) = "maybe another function definition"
end

You may also like

As the name suggests, this is a super minimal version of inline matching. I think it especially useful because of its simplicity. It won't give you any heavy dependency (excluding documentation the @match macro is only 15 lines long).

On the other hand, if you want something more flexible, more powerful, check out https://github.com/kmsquire/Match.jl.

Required Packages

No packages found.