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

ArgumentError in from_dict for Vectors and Strings #97

Open
xlxs4 opened this issue Dec 25, 2023 · 1 comment
Open

ArgumentError in from_dict for Vectors and Strings #97

xlxs4 opened this issue Dec 25, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@xlxs4
Copy link

xlxs4 commented Dec 25, 2023

When trying to parse a string in the TOML and convert it to a vector, it yields:

ERROR: ArgumentError: map(f, s::AbstractString) requires f to return AbstractChar; try map(f, collect(s)) or a comprehension instead

Hitting

from_dict(OptionType, of, eltype(T), each)

when trying to extend from_dict to work with a T<:AbstractVector and an S<:AbstractString.

MWE:

function axis2vec(s)
    axis2vec = Dict(
        "+X" => Vector(1.0, 0, 0),
        "-X" => Vector(-1.0, 0, 0),
        "+Y" => Vector(0, 1.0, 0),
        "-Y" => Vector(0, -1.0, 0),
        "+Z" => Vector(0, 0, 1.0),
        "-Z" => Vector(0, 0, -1.0),
    )
    return axis2vec[axis]
end

    
@option struct Example
    xs::Vector{Float64}
end

Configurations.from_dict(::Type{Example}, ::Type{Vector{Float64}}, x::AbstractString) = axis2vec(x)
@Roger-luo Roger-luo added the bug Something isn't working label Jan 1, 2024
@xlxs4
Copy link
Author

xlxs4 commented Feb 12, 2024

For anyone interested, I'm currently circumventing this by doing something similar to:

struct Example
    xs::Vector{Float64}
end

@option struct _Example
    xs::String
end

_from_dict(t, d) = from_dict(t, d)
function _from_dict(t::Type{_Example}, d)
    _ex = from_dict(t, d)
    return Example(axis2vec(_ex.xs)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants