Skip to content

Commit 55fdd9f

Browse files
authored
Simplify implementation of Algorithm(::Symbol) (#224)
1 parent e89fa28 commit 55fdd9f

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

src/NLopt.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,12 @@ end
6363
Base.convert(::Type{nlopt_algorithm}, a::Algorithm) = nlopt_algorithm(Int(a))
6464
Base.convert(::Type{Algorithm}, r::nlopt_algorithm) = Algorithm(Int(r))
6565

66-
const _SYMBOL_TO_ALGORITHM = Dict(Symbol(i) => i for i in instances(Algorithm))
67-
68-
function Algorithm(name::Symbol)
69-
algorithm = get(_SYMBOL_TO_ALGORITHM, name, nothing)
70-
if algorithm === nothing
71-
throw(ArgumentError("unknown algorithm $name"))
66+
function Algorithm(name::Symbol)::Algorithm
67+
algorithm = nlopt_algorithm_from_string("$name")
68+
if UInt32(algorithm) == 0xffffffff
69+
throw(ArgumentError("unknown algorithm: $name"))
7270
end
73-
return algorithm::Algorithm
71+
return algorithm
7472
end
7573

7674
# enum nlopt_result

test/C_API.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ function test_issue_156_no_error()
7575
end
7676

7777
function test_invalid_algorithms()
78-
@test_throws ArgumentError("unknown algorithm BILL") Algorithm(:BILL)
79-
@test_throws ArgumentError("unknown algorithm BILL") Opt(:BILL, 420)
78+
@test_throws ArgumentError("unknown algorithm: BILL") Algorithm(:BILL)
79+
@test_throws ArgumentError("unknown algorithm: BILL") Opt(:BILL, 420)
8080
return
8181
end
8282

0 commit comments

Comments
 (0)