Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify implementation of Algorithm(::Symbol) #224

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading