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

Check does field have default defined? #157

Open
ahojukka5 opened this issue Jan 15, 2024 · 0 comments
Open

Check does field have default defined? #157

ahojukka5 opened this issue Jan 15, 2024 · 0 comments

Comments

@ahojukka5
Copy link

Can I verify whether a specific field has a default value? I'm looking to dynamically construct a struct and configure it using a named tuple, but I want to do this only for fields that don't have predefined defaults.

@with_kw struct Model
    Lx::Int=128
    Ly::Int=128
    M::Matrix
end

function has_default(Model, fieldname)
    # ....?
end

function create(Model, settings)
    defaults = Dict()
    for (fieldname, fieldtype) in zip(fieldnames(Model), fieldtypes(Model))
        if fieldtype == Matrix && !has_default(Model, fieldname)
            defaults[fieldname] = fieldtype(undef, settings.Lx, settings.Ly)
        end
    end
    return Model(; settings..., defaults...)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant