From 5e3f6c8b2f97adcbfac957a2eb11f813a9c26b86 Mon Sep 17 00:00:00 2001 From: lrennels Date: Sun, 14 Nov 2021 19:35:56 -0800 Subject: [PATCH 1/2] Allow setting a default Symbol or String --- src/core/defcomp.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/core/defcomp.jl b/src/core/defcomp.jl index 3e0f7b7ac..c00117d6b 100644 --- a/src/core/defcomp.jl +++ b/src/core/defcomp.jl @@ -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) From 7e1843e8a371d554fea23abca50e06282a054334 Mon Sep 17 00:00:00 2001 From: lrennels Date: Sun, 14 Nov 2021 19:49:00 -0800 Subject: [PATCH 2/2] Add testing --- test/test_defaults.jl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/test_defaults.jl b/test/test_defaults.jl index 289f823ba..680b69f2c 100644 --- a/test/test_defaults.jl +++ b/test/test_defaults.jl @@ -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 \ No newline at end of file