File tree Expand file tree Collapse file tree 2 files changed +33
-2
lines changed Expand file tree Collapse file tree 2 files changed +33
-2
lines changed Original file line number Diff line number Diff line change @@ -62,12 +62,12 @@ function _process_model_def(modl, ex)
62
62
if line. head == :(= ) # assignment for default
63
63
default = line. args[2 ]
64
64
# if a constraint is given (value::constraint)
65
- if default isa Expr && length ( default. args) > 1
65
+ if default isa Expr && default. head == :( :: )
66
66
constraints[param] = default. args[2 ]
67
67
# now discard the constraint to keep only the value
68
68
default = default. args[1 ]
69
69
end
70
- defaults[param] = default # this will be a value not an expr
70
+ defaults[param] = default
71
71
72
72
# name or name::Type (for the constructor)
73
73
ex. args[3 ]. args[i] = line. args[1 ]
Original file line number Diff line number Diff line change 151
151
@test Cc (). a === nothing
152
152
@test Cd (). a === missing
153
153
end
154
+
155
+ @testset " Expression defaults" begin
156
+ # Should work with and without constraint:
157
+ @mlj_model mutable struct Foo1
158
+ a:: Vector{Int} = [1 , 2 , 3 ]
159
+ end
160
+ @test Foo1 (). a == [1 , 2 , 3 ]
161
+ @mlj_model mutable struct Foo2
162
+ a:: Vector{Int} = [1 , 2 , 3 ]:: (true)
163
+ end
164
+ @test Foo2 (). a == [1 , 2 , 3 ]
165
+
166
+ # Constraints applied
167
+ @mlj_model mutable struct Foo3
168
+ a:: Vector{Int} = [1 , 2 , 3 ]:: (all(>(0) , _))
169
+ end
170
+ @test redirect_stderr (devnull ) do
171
+ Foo3 (; a = [- 1 ]). a == [1 , 2 , 3 ]
172
+ end
173
+
174
+ # Negative number:
175
+ @mlj_model mutable struct Foo4
176
+ a:: Float64 = - 1.0
177
+ end
178
+ @test Foo4 (). a === - 1.0
179
+ @mlj_model mutable struct Foo5
180
+ a:: Float64 = (- 1.0 ):: (true)
181
+ end
182
+ @test Foo5 (). a == - 1.0
183
+
184
+ end
You can’t perform that action at this time.
0 commit comments