Skip to content

Commit

Permalink
Merge pull request #71 from alan-turing-institute/eval-in-closed-module
Browse files Browse the repository at this point in the history
Fix "eval in closed module" issue
  • Loading branch information
ablaom authored Oct 14, 2020
2 parents 4ab2762 + 49baa47 commit 594e8c6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ os:
- linux
julia:
- 1.0
- 1.4
- 1.5
- nightly
matrix:
allow_failures:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MLJModelInterface"
uuid = "e80e1ace-859a-464e-9ed9-23947d8ae3ea"
authors = ["Thibaut Lienart and Anthony Blaom"]
version = "0.3.5"
version = "0.3.6"

[deps]
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand Down
21 changes: 13 additions & 8 deletions src/model_def.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
clean!(::Model) = ""

"""
_process_model_def(ex)
_process_model_def(modl, ex)
Take an expression defining a model (`mutable struct Model ...`) and unpack key
elements for further processing:
Expand All @@ -17,12 +17,17 @@ elements for further processing:
- Names of parameters (`params`)
- Default values (`defaults`)
- Constraints (`constraints`)
"""
function _process_model_def(ex)
defaults = Dict{Symbol,Any}()
When no default field value is given a heuristic is to guess an
appropriate default (eg, zero for a `Float64` parameter). To this end,
the specified type expression is evaluated in the module `modl`.
"""
function _process_model_def(modl, ex)
defaults = Dict{Symbol,Any}()
constraints = Dict{Symbol,Any}()
modelname = ex.args[2] isa Symbol ? ex.args[2] : ex.args[2].args[1]
params = Symbol[]
modelname = ex.args[2] isa Symbol ? ex.args[2] : ex.args[2].args[1]
params = Symbol[]

# inspect all lines which may define parameters, retrieve their names,
# default values and constraints on values that can be given to them
Expand Down Expand Up @@ -67,7 +72,7 @@ function _process_model_def(ex)
# these are simple heuristics when no default value is given for the
# field but an "obvious" one can be provided implicitly (ideally this should
# not be used as it's not very clear that the intention matches the usage)
eff_type = eval(type)
eff_type = modl.eval(type)
if eff_type <: Number
defaults[param] = zero(eff_type)
elseif eff_type <: AbstractString
Expand Down Expand Up @@ -168,7 +173,7 @@ end
Macro to help define MLJ models with constraints on the default parameters.
"""
macro mlj_model(ex)
ex, modelname, params, defaults, constraints = _process_model_def(ex)
ex, modelname, params, defaults, constraints = _process_model_def(__module__, ex)
# keyword constructor
const_ex = _model_constructor(modelname, params, defaults)
# associate the constructor with the definition of the struct
Expand Down

0 comments on commit 594e8c6

Please sign in to comment.