Skip to content

Commit

Permalink
Simplify implementation of Algorithm(::Symbol) (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Aug 20, 2024
1 parent e89fa28 commit 55fdd9f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
12 changes: 5 additions & 7 deletions src/NLopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/C_API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 55fdd9f

Please sign in to comment.