Skip to content

043 Inline Construction Function ​

043-inline-construction-function.mc uses a single-channel Pull method and creates its helper component inside the call:

text
R_PULL::PULL_RESISTOR().Pull([BUTTON_IN, V3V3])

R_PULL::PULL_RESISTOR() means "make an instance named R_PULL of type PULL_RESISTOR here." The :: in this expression separates an instance name from a component type. That is different from the interface binding use of :: in chapter 03, where UART0::UART.TTL(DCE) bound pins to an interface.

This example returns to one pull-up resistor and one button so the inline construction syntax remains the only new idea. Its method omits return because the call result is not used.

bash
eval "$(../mcc/scripts/mcc-slot.sh)"
MCC_SYSTEM_ROOT="$(cd .. && pwd)" "$MCC_BIN" --local parse 04-functions-and-reuse/043-inline-construction-function.mc --lib mcode --pass1 --pass2
MCC_SYSTEM_ROOT="$(cd .. && pwd)" "$MCC_BIN" --local parse 04-functions-and-reuse/043-inline-construction-function.mc --lib mcode --viz -o 04-functions-and-reuse/043-inline-construction-function.html

Source ​

text
// Example: Inline Construction Function
// Goal: Name a helper component inside the method-call expression.
// Language focus: NAME::TYPE() inline construction.

component PULL_RESISTOR
{
    name = "Pull-Up Resistor"
    pins = [
        1 = INPUT
        2 = SOURCE
    ]

    func Pull([input, source])
    {
        input -> this -> source
    }
}

module main
{
    io BUTTON_IN
    io GND
    io V3V3

    DC.SRC PWR(3.3V, 50mA)
    SWITCH.MOM SW_USER

    PWR.1 -> V3V3
    PWR.2 -> GND

    R_PULL::PULL_RESISTOR().Pull([BUTTON_IN, V3V3])
    BUTTON_IN -> SW_USER.COM
    SW_USER.NO -> GND
}

MCode Bench is the public face of the MCode stack. Closed capabilities are served through the MCode gateway.