Skip to content

Commit

Permalink
First Attempt (#3)
Browse files Browse the repository at this point in the history
* Basics

* Add inference test

* Only run on 1.10

* Add inner constructor

* Enable default constructor

* Ensure that default constructor works

* Add small example in readme

* Fix typo

* Set to version 1

* Tweak Project.toml
  • Loading branch information
willtebbutt authored May 31, 2024
1 parent a2825b6 commit ee52272
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ jobs:
matrix:
version:
- '1.10'
- '1.6'
- 'nightly'
os:
- ubuntu-latest
arch:
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MistyClosures"
uuid = "dbe65cb8-6be2-42dd-bbc5-4196aaced4f4"
authors = ["Will Tebbutt", "Frames White", "and Hong Ge"]
version = "1.0.0-DEV"
authors = ["Will Tebbutt", "Frames White", "Hong Ge"]
version = "1.0.0"

[compat]
julia = "1.10"
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,32 @@
[![Build Status](https://github.com/compintell/MistyClosures.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/compintell/MistyClosures.jl/actions/workflows/CI.yml?query=branch%3Amain)
[![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle)
[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor's%20Guide-blueviolet)](https://github.com/SciML/ColPrac)

Marginally less opaque closures.

Specifically, a `MistyClosure` is comprises an `OpaqueClosure` and an `IRCode`.
This is useful if you generate an `OpaqueClosure` from an `IRCode`, and want to be able to retrieve the `IRCode` later on.

## Recommended Use

```julia
# Get the `IRCode` associated to `sin(5.0)`.
ir = Base.code_ircode_by_type(Tuple{typeof(sin), Float64})[1][1]

# Produce a `MistyClosure` using it. All kwargs are passed to the `OpaqueClosure`
# constructor.
mc = MistyClosure(ir; do_compile=true)

# Call it.
mc(5.0) == sin(5.0)
```

## Alterative Use

Sometimes you'll already have an `OpaqueClosure` lying around, and not want to produce a new one from an `IRCode` (as this often takes a surprisingly large amount of time).
If ths is the case, you can simply use the default constructor for `MistyClosure`.
That is, write
```julia
mc = MistyClosure(existing_opaque_closure, ir)
```
Of course, it is _your_ responsibility so ensure that `ir` and `existing_opaque_closure` are in agreement.
14 changes: 13 additions & 1 deletion src/MistyClosures.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
module MistyClosures

# Write your package code here.
using Core: OpaqueClosure
using Core.Compiler: IRCode

struct MistyClosure{Toc<:OpaqueClosure}
oc::Toc
ir::IRCode
end

MistyClosure(ir::IRCode; kwargs...) = MistyClosure(OpaqueClosure(ir; kwargs...), ir)

(mc::MistyClosure)(x::Vararg{Any, N}) where {N} = mc.oc(x...)

export MistyClosure

end
15 changes: 12 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
using MistyClosures
using Test
using MistyClosures, Test

using Core: OpaqueClosure

@testset "MistyClosures.jl" begin
# Write your tests here.
ir = Base.code_ircode_by_type(Tuple{typeof(sin), Float64})[1][1]

# Recommended constructor.
mc = MistyClosure(ir; do_compile=true)
@test @inferred(mc(5.0)) == sin(5.0)

# Default constructor.
mc_default = MistyClosure(OpaqueClosure(ir; do_compile=true), ir)
@test @inferred(mc_default(5.0) == sin(5.0))
end

5 comments on commit ee52272

@willtebbutt
Copy link
Member Author

Choose a reason for hiding this comment

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

@willtebbutt
Copy link
Member Author

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.

Error while trying to register: Register Failed
@willtebbutt, it looks like you are not a publicly listed member/owner in the parent organization (compintell).
If you are a member/owner, you will need to change your membership to public. See GitHub Help

@willtebbutt
Copy link
Member Author

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 created: JuliaRegistries/General/108007

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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 v1.0.0 -m "<description of version>" ee522725df94c46c987c7e3e9a0ccf8f9c8fa6a6
git push origin v1.0.0

Please sign in to comment.