Skip to content

Commit

Permalink
First version of the library
Browse files Browse the repository at this point in the history
  • Loading branch information
dhanak committed Aug 4, 2023
1 parent b131a5b commit e85fc5c
Show file tree
Hide file tree
Showing 11 changed files with 607 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ master ]
pull_request:
branches: [ master ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
strategy:
matrix:
# Test with Julia 1.6 (lowest compat version) and latest Julia 1.*
julia-version: ["1.6", "1"]

# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/checkout@v3

- name: Setup Julia environment
uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.julia-version }}

- name: Run Pkg.build
uses: julia-actions/julia-buildpkg@v1

- name: Run Julia package tests
uses: julia-actions/julia-runtest@v1

- name: Compute test coverage
uses: julia-actions/julia-processcoverage@v1

- name: Report coverage
uses: codecov/codecov-action@v3
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (C) The TidyTest.jl contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions Manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file is machine-generated - editing it directly is not advised

julia_version = "1.8.5"
manifest_format = "2.0"
project_hash = "b49e9b1483a70ece1f89d014cdbe39fe7ec7e18e"

[deps]
7 changes: 7 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name = "InlineDispatch"
uuid = "ab49173f-ff2e-4091-9f72-02325fb05285"
authors = ["David Hanak <[email protected]>"]
version = "0.1.0"

[compat]
julia = "1.6"
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[![CI](https://github.com/dhanak/InlineDispatch.jl/actions/workflows/CI.yml/badge.svg)](https://github.com/dhanak/InlineDispatch.jl/actions/workflows/CI.yml)
[![codecov](https://codecov.io/gh/dhanak/InlineDispatch.jl/branch/master/graph/badge.svg?token=CQYSC7NLOT)](https://codecov.io/gh/dhanak/InlineDispatch.jl)
[![Aqua QA](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl)

# InlineDispatch.jl

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

```julia
@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
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!
```

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

```julia
julia> try
do_some_stuff()
catch exn
@dispatch exn begin
e::AssertionError -> println(stderr, "AssertionError: ", e.msg)
::InexactError -> println(stderr, "InexactError")
_ -> rethrow()
end
end
```
167 changes: 167 additions & 0 deletions Test/Manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# This file is machine-generated - editing it directly is not advised

julia_version = "1.8.5"
manifest_format = "2.0"
project_hash = "75d3818b2b3e4a4e0ec74093f7387279b8f87dc2"

[[deps.Aqua]]
deps = ["Compat", "Pkg", "Test"]
git-tree-sha1 = "841cdd09bf3f6ad40ab16b8dc3b6a7a19ce298a3"
uuid = "4c88cf16-eb10-579e-8560-4a9242c79595"
version = "0.6.5"

[[deps.ArgTools]]
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
version = "1.1.1"

[[deps.Artifacts]]
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"

[[deps.Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[deps.Compat]]
deps = ["Dates", "LinearAlgebra", "UUIDs"]
git-tree-sha1 = "5ce999a19f4ca23ea484e92a1774a61b8ca4cf8e"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "4.8.0"

[[deps.CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
version = "1.0.1+0"

[[deps.Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[deps.Downloads]]
deps = ["ArgTools", "FileWatching", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
version = "1.6.0"

[[deps.FileWatching]]
uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee"

[[deps.InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[deps.LibCURL]]
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"
version = "0.6.3"

[[deps.LibCURL_jll]]
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"]
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"
version = "7.84.0+0"

[[deps.LibGit2]]
deps = ["Base64", "NetworkOptions", "Printf", "SHA"]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"

[[deps.LibSSH2_jll]]
deps = ["Artifacts", "Libdl", "MbedTLS_jll"]
uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8"
version = "1.10.2+0"

[[deps.Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[deps.LinearAlgebra]]
deps = ["Libdl", "libblastrampoline_jll"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[[deps.Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[deps.Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[deps.MbedTLS_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
version = "2.28.0+0"

[[deps.MozillaCACerts_jll]]
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
version = "2022.2.1"

[[deps.NetworkOptions]]
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"
version = "1.2.0"

[[deps.OpenBLAS_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"]
uuid = "4536629a-c528-5b80-bd46-f80d51c5b363"
version = "0.3.20+0"

[[deps.Pkg]]
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
version = "1.8.0"

[[deps.Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[deps.REPL]]
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

[[deps.Random]]
deps = ["SHA", "Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[deps.SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
version = "0.7.0"

[[deps.Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[deps.Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[deps.TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
version = "1.0.0"

[[deps.Tar]]
deps = ["ArgTools", "SHA"]
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
version = "1.10.1"

[[deps.Test]]
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[deps.UUIDs]]
deps = ["Random", "SHA"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[[deps.Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[[deps.Zlib_jll]]
deps = ["Libdl"]
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"
version = "1.2.12+3"

[[deps.libblastrampoline_jll]]
deps = ["Artifacts", "Libdl", "OpenBLAS_jll"]
uuid = "8e850b90-86db-534c-a0d3-1478176c7d93"
version = "5.1.1+0"

[[deps.nghttp2_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"
version = "1.48.0+0"

[[deps.p7zip_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
version = "17.4.0+0"
2 changes: 2 additions & 0 deletions Test/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[deps]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
78 changes: 78 additions & 0 deletions src/InlineDispatch.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
A simple module to perform dispatch on a value of an expression using the
`@dispatch` macro.
"""
module InlineDispatch

export @dispatch

"""
@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
```jldoctest
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!
```
It can be particularly useful in `try ... catch` blocks to handle various types
of errors.
```julia-repl
julia> try
do_some_stuff()
catch exn
@dispatch exn begin
e::AssertionError -> println(stderr, "AssertionError: ", e.msg)
::InexactError -> println(stderr, "InexactError")
_ -> rethrow()
end
end
```
"""
macro dispatch(expr, body)
@assert(body isa Expr && body.head == :block,
"begin ... end block expected in second argument!")
fn = gensym("dispatch")
methods::Vector{Union{Expr, LineNumberNode}} = map(body.args) do ex
ex isa LineNumberNode && return ex
@assert ex isa Expr && ex.head == :-> "Anonymous function expected!"
return :($fn($(ex.args[1])) = $(ex.args[2]))
end
return quote
let $fn(::T) where {T} = error("@dispatch: Unmatched type $(T)!")
$(methods...)
$fn($expr)
end
end |> esc
end

end # module InlineDispatch
Loading

2 comments on commit e85fc5c

@dhanak
Copy link
Owner Author

@dhanak dhanak commented on e85fc5c Aug 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/89012

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" e85fc5cea59410208e6e9554506bca87cc752490
git push origin v0.1.0

Please sign in to comment.