GithubHelp home page GithubHelp logo

dhanak / inlinedispatch.jl Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 0.0 13 KB

A simple Julia module to perform dispatch on a value of an expression using the `@dispatch` macro.

License: MIT License

Julia 100.00%
dispatch error-handling try-catch julia

inlinedispatch.jl's Introduction

CI codecov Aqua QA

InlineDispatch.jl

A simple module to perform dispatch on the value of an expression using the @dispatch macro.

    @dispatch expr begin
        v::Type1 -> body1...
        v::Type2 -> body2...
    end

Perform a dispatch on the value of expr.

The dispatch uses the anonymous functions in the block as methods, and returns the value of the appropriate body expression. The order of the functions doesn't matter, the most specific match is chosen, as customary with Julian dispatch.

Examples

julia> @dispatch 42 begin
           i::Integer -> "int $i"
           r::Real    -> "real $r"
           ::Nothing  -> "nothing"
       end
"int 42"

julia> @dispatch π begin
           i::Integer -> "int $i"
           r::Real    -> "real $r"
           ::Nothing  -> "nothing"
       end
"real π"

julia> @dispatch "foo" begin
           i::Integer -> "int $i"
           r::Real    -> "real $r"
           ::Nothing  -> "nothing"
       end
ERROR: @dispatch: Unmatched type String! @ REPL[3]:1

It can be particularly useful in try ... catch blocks to handle various types of errors.

julia> try
           do_some_stuff()
       catch exn
           @dispatch exn begin
               e::AssertionError -> println(stderr, "AssertionError: ", e.msg)
               ::InexactError    -> println(stderr, "InexactError")
               _                 -> rethrow()
           end
       end

inlinedispatch.jl's People

Contributors

dhanak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.