Add foam.DEF_MACRO and foam.APPLY_MACRO #1869
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Meta-programming is already possible in FOAM by calling methods like
foam.CLASS
dynamically. This PR introduces explicit meta-programming support so that this behavior can be better controlled by FOAM.The following example defines a macro that creates two models for each value it's applied to:
We can then imply models
GreedyCat
,LazyCat
,GreedyDog
andLazyDog
as follows:In order to justify adding explicit support, the need for meta-programming is a prerequisite justification. Sometimes it makes sense to generate individual models from tabular data in a similar fashion that FOAM can already generate models from XSD files. I came across a good example while modelling WebAssembly functions. Each instruction has different properties and creating a general model to handle all cases would add complexity and prevent adding refinements to individual instructions.
With
foam.APPLY_MACRO
I was able to represent the differences between instructions concisely in a few lines of code:Here are some advantages of explicit support as opposed to the meta-programming that is already possible:
_MACRO
, you know meta-programming is happening.documentation
can be added to the macro itself