-
Notifications
You must be signed in to change notification settings - Fork 25
Allow External Unit Registration #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
04d2b14
feat: allow registering units externally
ven-k 2c869e3
fix: import of vars into SymbolicUnits sybmodule
ven-k 5c00b03
feat: add `WriteOnceReadMany` and utils
ven-k cbd071c
test: precompile `@register_unit` in an external module.
ven-k b44749f
Clean up `write_once_read_many.jl`
MilesCranmer 464d5a7
Clean up ambiguity calculation
MilesCranmer f51b554
Simplify WriteOnceReadMany
MilesCranmer 6a75e3c
Fix return value from `Base.push!`
MilesCranmer 1fc8290
Fix return value from `Base.setindex!`
MilesCranmer 8f2cc24
Move INDEX_TYPE back to symbolic_dimensions
MilesCranmer 9b1cba4
Add docstring for `@register_unit`
MilesCranmer abbcdec
Improve @register_unit docstring and add to docs
MilesCranmer ac1889a
Type stability
MilesCranmer d6f5cc1
Clean up diff
MilesCranmer 81321a0
Improve docstring of WriteOnceReadMany
MilesCranmer a1ba7e7
Stylistic tweaks
MilesCranmer 92df2f4
Only test error string on 1.9+
MilesCranmer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import .Units: UNIT_MAPPING, UNIT_SYMBOLS, UNIT_VALUES, _lazy_register_unit | ||
import .SymbolicUnits: update_external_symbolic_unit_value | ||
|
||
# Update the unit collections | ||
const UNIT_UPDATE_LOCK = Threads.SpinLock() | ||
|
||
function update_all_values(name_symbol, unit) | ||
MilesCranmer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lock(UNIT_UPDATE_LOCK) do | ||
push!(ALL_SYMBOLS, name_symbol) | ||
push!(ALL_VALUES, unit) | ||
i = lastindex(ALL_VALUES) | ||
ALL_MAPPING[name_symbol] = i | ||
UNIT_MAPPING[name_symbol] = i | ||
update_external_symbolic_unit_value(name_symbol) | ||
end | ||
end | ||
|
||
""" | ||
@register_unit symbol value | ||
|
||
Register a new unit under the given symbol to have | ||
a particular value. | ||
|
||
# Example | ||
|
||
```julia | ||
julia> @register_unit MyVolt 1.5u"V" | ||
``` | ||
|
||
This will register a new unit `MyVolt` with a value of `1.5u"V"`. | ||
You can then use this unit in your calculations: | ||
|
||
```julia | ||
julia> x = 20us"MyVolt^2" | ||
20.0 MyVolt² | ||
|
||
julia> y = 2.5us"A" | ||
2.5 A | ||
|
||
julia> x * y^2 |> uconvert(us"W^2") | ||
281.25 W² | ||
|
||
julia> x * y^2 |> uconvert(us"W^2") |> sqrt |> uexpand | ||
16.77050983124842 m² kg s⁻³ | ||
``` | ||
|
||
""" | ||
macro register_unit(symbol, value) | ||
return esc(_register_unit(symbol, value)) | ||
end | ||
|
||
function _register_unit(name::Symbol, value) | ||
name_symbol = Meta.quot(name) | ||
index = get(ALL_MAPPING, name, INDEX_TYPE(0)) | ||
if !iszero(index) | ||
unit = ALL_VALUES[index] | ||
MilesCranmer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# When a utility function to expand `value` to its final form becomes | ||
# available, enable the following check. This will avoid throwing an error | ||
# if user is trying to register an existing unit with matching values. | ||
# unit.value != value && throw("Unit $name is already defined as $unit") | ||
MilesCranmer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
error("Unit `$name` is already defined as `$unit`") | ||
end | ||
reg_expr = _lazy_register_unit(name, value) | ||
push!( | ||
reg_expr.args, | ||
quote | ||
$update_all_values($name_symbol, $value) | ||
nothing | ||
end | ||
) | ||
return reg_expr | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.