Skip to content
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "IrrationalConstants"
uuid = "92d709cd-6900-40b7-9082-c6be49f344b6"
authors = ["JuliaMath"]
version = "0.2.5"
version = "0.2.6"

[compat]
julia = "1.10"
Expand Down
15 changes: 12 additions & 3 deletions src/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ julia> IrrationalConstants.@irrational twoπ 2*big(π)
julia> twoπ
twoπ = 6.2831853071795...

julia> typeof(twoπ)
Twoπ

julia> IrrationalConstants.@irrational sqrt2 1.4142135623730950488 √big(2)

julia> sqrt2
Expand All @@ -65,12 +68,18 @@ ERROR: AssertionError: Float64($(Expr(:escape, :sqrt5))) == Float64(big($(Expr(:
```
"""
macro irrational(sym::Symbol, val::Float64, def::Union{Symbol,Expr}, T::Symbol=Symbol(uppercasefirst(string(sym))))
irrational(sym, val, def, T)
irrational(__module__, sym, val, def, T)
end
macro irrational(sym::Symbol, def::Union{Symbol,Expr}, T::Symbol=Symbol(uppercasefirst(string(sym))))
irrational(sym, :(big($(esc(sym)))), def, T)
irrational(__module__, sym, :(big($(esc(sym)))), def, T)
end
function irrational(sym::Symbol, val::Union{Float64,Expr}, def::Union{Symbol,Expr}, T::Symbol)
function irrational(mod::Module, sym::Symbol, val::Union{Float64,Expr}, def::Union{Symbol,Expr}, T::Symbol)
if isdefined(mod, T)
throw(ArgumentError(LazyString("Type `", T, "` of irrational constant `", sym, "` is already defined in module `", mod, "`.")))
end
if sym == T
throw(ArgumentError(LazyString("The name of the irrational constant (", sym, ") and its type (", T, ") cannot be the same. Please choose a different name for the constant or specify a different type name as the last argument to the macro.")))
end
esym = esc(sym)
qsym = esc(Expr(:quote, sym))
eT = esc(T)
Expand Down
24 changes: 24 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,27 @@ end
@testset "slow comparisons" begin
@test iszero(@allocated(3.0 <= invsqrt2))
end

# issues #43
@testset "macro error" begin
msg = "The name of the irrational constant (Myπ) and its type (Myπ) cannot be the same. Please choose a different name for the constant or specify a different type name as the last argument to the macro."
@test_throws ArgumentError(msg) @macroexpand(IrrationalConstants.@irrational Myπ big(π))
@test_throws ArgumentError(msg) @macroexpand(IrrationalConstants.@irrational Myπ 1.0 big(π))
@test_throws ArgumentError(msg) @macroexpand(IrrationalConstants.@irrational Myπ big(π) Myπ)
@test_throws ArgumentError(msg) @macroexpand(IrrationalConstants.@irrational Myπ 1.0 big(π) Myπ)
end

# test that defining a type that already exists throws an error
module TestTypeCollision
using IrrationalConstants
using Test
struct MyExistingType end

@testset "type collision" begin
msg1 = "Type `MyExistingType` of irrational constant `myExistingType` is already defined in module `Main.TestTypeCollision`."
@test_throws ArgumentError(msg1) @macroexpand(IrrationalConstants.@irrational myExistingType big(π))
msg2 = "Type `MyExistingType` of irrational constant `myconst` is already defined in module `Main.TestTypeCollision`."
@test_throws ArgumentError(msg2) @macroexpand(IrrationalConstants.@irrational myconst big(π) MyExistingType)
@test_throws ArgumentError(msg2) @macroexpand(IrrationalConstants.@irrational myconst 1.0 big(π) MyExistingType)
end
end