Created
September 12, 2022 22:48
-
-
Save singularitti/b47578262a83b7b2fd65116760e2c391 to your computer and use it in GitHub Desktop.
Toggle constant in Julia #Julia #toggle
This file contains 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
module ToggleableAsserts | |
export @toggled_assert, toggle | |
assert_toggle() = true | |
macro toggled_assert(cond, text=nothing) | |
if text==nothing | |
assert_stmt = esc(:(@assert $cond)) | |
else | |
assert_stmt = esc(:(@assert $cond $text)) | |
end | |
:(assert_toggle() ? $assert_stmt : nothing) | |
end | |
const toggle_lock = ReentrantLock() | |
function toggle(enable::Bool) | |
lock(toggle_lock) do | |
@eval ToggleableAsserts assert_toggle() = $enable | |
on_or_off = enable ? "on." : "off." | |
@info "Toggleable asserts turned "*on_or_off | |
end | |
end | |
end # module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment