Skip to content

Instantly share code, notes, and snippets.

@singularitti
Created September 12, 2022 22:48
Show Gist options
  • Save singularitti/b47578262a83b7b2fd65116760e2c391 to your computer and use it in GitHub Desktop.
Save singularitti/b47578262a83b7b2fd65116760e2c391 to your computer and use it in GitHub Desktop.
Toggle constant in Julia #Julia #toggle
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