Skip to content

Commit

Permalink
Merge pull request #874 from mimiframework/default-symbol
Browse files Browse the repository at this point in the history
Allow setting a default Symbol or String
  • Loading branch information
lrennels authored Nov 17, 2021
2 parents 5c683a5 + 943fd46 commit 288c1e2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/core/defcomp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ end

function add_parameter(comp_def::ComponentDef, name, datatype, dimensions, description, unit, default)
if default !== nothing
if length(dimensions) != ndims(default)
error("Default value has different number of dimensions ($(ndims(default))) than parameter '$name' ($(length(dimensions)))")
ndims_default = (default isa Symbol || default isa String) ? 0 : ndims(default)
if length(dimensions) != ndims_default
error("Default value has different number of dimensions ($(ndims_default))) than parameter '$name' ($(length(dimensions)))")
end
end
p = ParameterDef(name, comp_def.comp_path, datatype, dimensions, description, unit, default)
Expand Down
14 changes: 14 additions & 0 deletions test/test_defaults.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,18 @@ run(m)
@test m[:A, :p1] == 40
@test Mimi.model_param(m, :model_p1).value == 30

# try setting defaults for Strings or Symbols
@defcomp A begin
p1 = Parameter{String}(default = "hello")
p2 = Parameter{Symbol}(default = :goodbye)
end

m = Model()
set_dimension!(m, :time, 1:10)
add_comp!(m, A)
run(m)

@test m[:A, :p1] == "hello"
@test m[:A, :p2] == :goodbye

end

0 comments on commit 288c1e2

Please sign in to comment.