-
Notifications
You must be signed in to change notification settings - Fork 25
Description
I am trying to parse user-input for a simulation package I am writing.
For that I want the user to be able to give values with units defined in text input files, read in the input, and convert the input to SI units.
This mostly works, as long as the provided units are defined in the Units submodule of DynamicQuantities.jl,
using DynamicQuantities
# Read in from input file
data = Dict("value" => 100, "unit" => "J")
value = data["value"]
unit = data["unit"]
ret = uparse("$(value)$(unit)")
# 100.0 m² kg s⁻²
but fails when the unit includes something only defined in the Constants submodule.
using DynamicQuantities
# Read in from input file
data = Dict("value" => 100, "unit" => "eV")
value = data["value"]
unit = data["unit"]
ret = uparse("$(value)$(unit)")
# ERROR: ArgumentError: Symbol eV found in `Constants` but not `Units`. Please use `u"Constants.eV"` instead.
Having a look into the implementation of uparse
(and sym_uparse
), I was wondering, why not just replace the throw(ArgumentError(...) in the elseif sym in CONSTANT_SYMBOLS
branch with the lookup_constants
call for the sym
with a the prefix 'Constants.':
function map_to_scope(sym::Symbol)
if sym in UNIT_SYMBOLS
# return at end
elseif sym in CONSTANT_SYMBOLS
# throw(ArgumentError("Symbol $sym found in `Constants` but not `Units`. Please use `us\"Constants.$sym\"` instead."))
return lookup_constant(Symbol("Constants.$(sym)"))
else
throw(ArgumentError("Symbol $sym not found in `Units` or `Constants`."))
end
return lookup_unit(sym)
end
From running the tests with this modifications, it seems that only these tests fail:
@test_throws "Symbol c found in `Constants` but not `Units`" uparse("c")
@test_throws "Symbol c found in `Constants` but not `Units`" sym_uparse("c")
Am I missing something else or would this be something which could be changed?
Or is there another way to avoid my problem?