Comps
Overview
Comps are specialized Axon functions that implement software components. Components are declared like other Axon functions but use a specialized syntax via the defcomp
keyword. Unlike straight functions, components do not have parameters or a return value. Instead data is input and output to the component using cells.
Cells are named variables with arbitrary meta data. Cells may be get/set externally to the component and are used to input/output data to the component's logic. Within the component's do
block cells are accessed and assigned as normal variables.
Syntax
Components are defined with the defcomp
keyword, followed by zero or more cell definitions and a do
block. Here is a simple component to sum two input numbers:
defcomp inA: {is:^number, defVal:0} inB: {is:^number, defVal:0} out: {is:^number, ro} do out = inA + inB end end
The defcomp
keyword starts the definition. Then we define three named cells along with their meta data. The cell meta is defined as a Dict literal where all the values must be literals (cannot be an expression). The do
block implements our computation logic. Inside the do
block the cells are accessed as normal variables.
Fantom
The following APIs are used to work with Axon components in Fantom:
Comp
: instance of componentCompDef
: definition of componentCellDef
: definition of component cellAbstractComp
: base class for components implemented in Fantom
You may implement custom components in Fantom in a HxLib. Your extension must advertise it contains Fantom components by putting the following line your pod's metadata in build.fan:
meta = [... "skyarc.fantomComps": "true", ]
Components are created as follows:
- Subclass from
AbstractComp
- Annotate your class with
Axon
facet for function meta - Declare constructor with one
Obj
arg - Declare cells as public fields with
Cell
facet - Implement
onRecompute
Here is a simple example:
@Axon { meta=Str<|dis:"Example Add"|> } class ExampleAddComp : AbstractComp { new make(Obj init) : super(init) {} @Cell { meta=Str<|dis:"A"|> } Number a := Number.zero @Cell { meta=Str<|dis:"B"|> } Number b := Number.zero @Cell { meta=Str<|ro|> } Number? out override Void onRecompute(AxonContext cx) { out = a + b } }
NOTE: the Fantom APIs for components are subject to change and are currently only available in SkySpark.
Calling
Comps can be called as normal functions. Pass a Dict with the input cells. The function will run the component's do block and return a Dict with all the cells. If we name our example from above "foo":
foo() // yields {inA:0, inB:0, out:0} foo({inA:2, inB:3}) // yields {inA:2, inB:3, out:5}
Note if you pass no Dict or omit cells then they will default to null or the value of the defVal
tag.
NOTE: this calling syntax is subject to change