From 55fdd9f42aaaef5c747ec7830a523cb5b4f1ee17 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 20 Aug 2024 12:02:34 +1200 Subject: [PATCH] Simplify implementation of Algorithm(::Symbol) (#224) --- src/NLopt.jl | 12 +++++------- test/C_API.jl | 4 ++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/NLopt.jl b/src/NLopt.jl index 3e476a2..d89a67e 100644 --- a/src/NLopt.jl +++ b/src/NLopt.jl @@ -63,14 +63,12 @@ end Base.convert(::Type{nlopt_algorithm}, a::Algorithm) = nlopt_algorithm(Int(a)) Base.convert(::Type{Algorithm}, r::nlopt_algorithm) = Algorithm(Int(r)) -const _SYMBOL_TO_ALGORITHM = Dict(Symbol(i) => i for i in instances(Algorithm)) - -function Algorithm(name::Symbol) - algorithm = get(_SYMBOL_TO_ALGORITHM, name, nothing) - if algorithm === nothing - throw(ArgumentError("unknown algorithm $name")) +function Algorithm(name::Symbol)::Algorithm + algorithm = nlopt_algorithm_from_string("$name") + if UInt32(algorithm) == 0xffffffff + throw(ArgumentError("unknown algorithm: $name")) end - return algorithm::Algorithm + return algorithm end # enum nlopt_result diff --git a/test/C_API.jl b/test/C_API.jl index b394d1e..ed712b0 100644 --- a/test/C_API.jl +++ b/test/C_API.jl @@ -75,8 +75,8 @@ function test_issue_156_no_error() end function test_invalid_algorithms() - @test_throws ArgumentError("unknown algorithm BILL") Algorithm(:BILL) - @test_throws ArgumentError("unknown algorithm BILL") Opt(:BILL, 420) + @test_throws ArgumentError("unknown algorithm: BILL") Algorithm(:BILL) + @test_throws ArgumentError("unknown algorithm: BILL") Opt(:BILL, 420) return end