Skip to content

Commit 56e47a0

Browse files
committed
Enforce type stability for u_str
1 parent 7b99fed commit 56e47a0

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/symbolic_dimensions.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,10 @@ module SymbolicUnits
400400
`Quantity(1.0, SymbolicDimensions, c=2, Hz=2)`. However, note that due to
401401
namespace collisions, a few physical constants are automatically converted.
402402
"""
403-
function sym_uparse(raw_string::AbstractString)
404-
raw_result = eval(map_to_scope(Meta.parse(raw_string)))
405-
return copy(as_quantity(raw_result))::DEFAULT_SYMBOLIC_QUANTITY_OUTPUT_TYPE
403+
function sym_uparse(s::AbstractString)
404+
ex = map_to_scope(Meta.parse(s))
405+
ex = :($as_quantity($ex))
406+
return copy(eval(ex))::DEFAULT_SYMBOLIC_QUANTITY_OUTPUT_TYPE
406407
end
407408

408409
as_quantity(q::DEFAULT_SYMBOLIC_QUANTITY_OUTPUT_TYPE) = q
@@ -445,9 +446,7 @@ module SymbolicUnits
445446
end
446447
end
447448

448-
import .SymbolicUnits: sym_uparse
449-
import .SymbolicUnits: SymbolicConstants
450-
import .SymbolicUnits: map_to_scope
449+
import .SymbolicUnits: as_quantity, sym_uparse, SymbolicConstants, map_to_scope
451450

452451
"""
453452
us"[unit expression]"
@@ -465,8 +464,9 @@ module. So, for example, `us"Constants.c^2 * Hz^2"` would evaluate to
465464
namespace collisions, a few physical constants are automatically converted.
466465
"""
467466
macro us_str(s)
468-
ex = Meta.parse(s)
469-
return esc(map_to_scope(ex))
467+
ex = map_to_scope(Meta.parse(s))
468+
ex = :($as_quantity($ex))
469+
return esc(ex)
470470
end
471471

472472
function Base.promote_rule(::Type{SymbolicDimensionsSingleton{R1}}, ::Type{SymbolicDimensionsSingleton{R2}}) where {R1,R2}

src/uparse.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ the quantity corresponding to the speed of light multiplied by Hertz,
3535
squared.
3636
"""
3737
function uparse(s::AbstractString)
38-
return as_quantity(eval(map_to_scope(Meta.parse(s))))::DEFAULT_QUANTITY_TYPE
38+
ex = map_to_scope(Meta.parse(s))
39+
ex = :($as_quantity($ex))
40+
return eval(ex)::DEFAULT_QUANTITY_TYPE
3941
end
4042

4143
as_quantity(q::DEFAULT_QUANTITY_TYPE) = q
@@ -55,8 +57,9 @@ the quantity corresponding to the speed of light multiplied by Hertz,
5557
squared.
5658
"""
5759
macro u_str(s)
58-
ex = Meta.parse(s)
59-
return esc(map_to_scope(ex))
60+
ex = map_to_scope(Meta.parse(s))
61+
ex = :($as_quantity($ex))
62+
return esc(ex)
6063
end
6164

6265
function map_to_scope(ex::Expr)

0 commit comments

Comments
 (0)