-
Notifications
You must be signed in to change notification settings - Fork 5
/
utilityMacros.jl
46 lines (44 loc) · 1.21 KB
/
utilityMacros.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#import Setfield
import FastClosures
import Accessors
"""
Helper function for the assignmacro, see @assign
We have one case where we assign a immutable structure to an immutable structure or something to a primitive variable.
If it is not a primitive we assign to a subcomponent of that structure. We then clone the structure with that particular field changed.
"""
function assignFunc(expr)
res =
if @capture(expr, lhs_._ = rhs_)
if !isprimitivetype(typeof(lhs))
Accessors.setmacro(identity, expr, overwrite=true)
else
quote
$(esc(expr))
end
end
else
quote
$(esc(expr))
end
end
return res
end
"""
This macro reimplements the MetaModelica assignment semantics using
setfield to assign to variables.
For assignments using primitives, the regular Julia assignment is generated.
For cases where deeply nested immutable structures are manipulated we use setfield
E.g.:
a.b.c = 5
Where a is a nested immutable struct
"""
macro assign(expr)
assignFunc(expr)
end
"""
Wraps the @closure macro of FastClosures.
See the FastClosure package for more information.
"""
macro closure(expr)
esc(FastClosures.wrap_closure(__module__, expr))
end