# functions

URL: /kida/api/compiler/statements/functions/
Section: statements
Description: Function statement compilation for Kida compiler.

Provides mixin for compiling function statements (def, call_block, slot).

Uses inline TYPE_CHECKING declarations for host attributes.
See: plan/rfc-mixin-protocol-typing.md

---

> For a complete page index, fetch /kida/llms.txt.

Open LLM text
(/kida/api/compiler/statements/functions/index.txt)

Share with AI

Ask Claude
(https://claude.ai/new?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstatements%2Ffunctions%2Findex.txt)

Ask ChatGPT
(https://chatgpt.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstatements%2Ffunctions%2Findex.txt)

Ask Gemini
(https://gemini.google.com/app?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstatements%2Ffunctions%2Findex.txt)

Ask Copilot
(https://copilot.microsoft.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstatements%2Ffunctions%2Findex.txt)

Module

#
`compiler.statements.functions`

Function statement compilation for Kida compiler.

Provides mixin for compiling function statements (def, call_block, slot).

Uses inline TYPE_CHECKING declarations for host attributes.
See: plan/rfc-mixin-protocol-typing.md

1Class1Function

## Classes

`FunctionCompilationMixin`

7

▼

Mixin for compiling function statements.

Host attributes and cross-mixin dependencies are declared…

Mixin for compiling function statements.

Host attributes and cross-mixin dependencies are declared via inline
TYPE_CHECKING blocks.

#### Methods

Internal Methods
7

▼

`_parse_annotation`

1

`ast.expr | None`

▼

Convert a raw annotation string to a Python AST expression node.

Uses ``ast.pa…

staticmethod

`def _parse_annotation(raw: str) -> ast.expr | None`

Convert a raw annotation string to a Python AST expression node.

Uses`ast.parse`in eval mode to parse annotation text like
`"str | None"`into the corresponding AST. Falls back to None
for malformed annotations.

##### Parameters

Name
Type
Description

`raw`
`—`

Annotation text from the template source.

##### Returns

`ast.expr | None`

AST expression node, or None if parsing fails.

`_make_callable_preamble`

1

`list[ast.stmt]`

▼

Build common local runtime preamble for callable codegen paths.

`def _make_callable_preamble(self, *, include_scope_stack: bool = False) -> list[ast.stmt]`

##### Parameters

Name
Type
Description

`include_scope_stack`
`—`

Default:`False`

##### Returns

`list[ast.stmt]`

`_compile_def`

1

`list[ast.stmt]`

▼

Compile {% def name(params) %}...{% enddef %.

Kida functions have true lexical…

`def _compile_def(self, node: Def) -> list[ast.stmt]`

Compile {% def name(params) %}...{% enddef %.

Kida functions have true lexical scoping - they can access variables
from their enclosing scope, unlike Jinja2 macros.

Generates:

```
def _def_name(arg1, arg2=default, *, _caller=None, _outer_ctx=ctx):
    buf = []
    ctx = {**_outer_ctx, 'arg1': arg1, 'arg2': arg2}
    if _caller:
        ctx['caller'] = _caller
    ... body ...
    return Markup(''.join(buf))
ctx['name'] = _def_name
```

##### Parameters

Name
Type
Description

`node`
`—`

##### Returns

`list[ast.stmt]`

`_compile_call_block`

1

`list[ast.stmt]`

▼

Compile {% call func(args) %}...{% endcall %.

Builds a caller that supports na…

`def _compile_call_block(self, node: CallBlock) -> list[ast.stmt]`

Compile {% call func(args) %}...{% endcall %.

Builds a caller that supports named slots. Passes _caller(slot="default")
so both _caller() and _caller("header_actions") work.

With scoped slots: slot functions accept **_slot_kwargs from the def-side
bindings and push them onto the scope stack so let: params are available
as local variables in the slot body.

##### Parameters

Name
Type
Description

`node`
`—`

##### Returns

`list[ast.stmt]`

`_make_region_function`

2

`tuple[list[ast.FunctionD…`

▼

Generate module-level region callable: _region_name(params, *, _outer_ctx) -> M…

`def _make_region_function(self, name: str, node: Region) -> tuple[list[ast.FunctionDef], ast.FunctionDef]`

Generate module-level region callable: _region_name(params, *, _outer_ctx) -> Markup.

Regions compile to both a callable (this) and a block wrapper.
No _caller/slots — regions are parameterized renderable units only.
Returns (thunk_defs, region_func) — thunks must be emitted before the region func.

##### Parameters

Name
Type
Description

`name`
`—`

`node`
`—`

##### Returns

`tuple[list[ast.FunctionDef], ast.FunctionDef]`

`_compile_region`

1

`list[ast.stmt]`

▼

Compile {% region name(params) %} for template body — emit ctx assign only.

`def _compile_region(self, node: Region) -> list[ast.stmt]`

##### Parameters

Name
Type
Description

`node`
`—`

##### Returns

`list[ast.stmt]`

`_compile_slot`

1

`list[ast.stmt]`

▼

Compile {% slot %} or {% slot name %} with optional scoped bindings.

Renders t…

`def _compile_slot(self, node: Slot) -> list[ast.stmt]`

Compile {% slot %} or {% slot name %} with optional scoped bindings.

Renders the caller content for the given slot name.
_caller(slot="default") supports both _caller() and _caller("name").

With scoped bindings: {% slot row let:item=item, let:index=loop.index %}

Passes bindings as keyword arguments to the caller function:

```
_caller("row", item=item, index=loop.index)
```

With body (default content): renders body when no caller is present.

##### Parameters

Name
Type
Description

`node`
`—`

##### Returns

`list[ast.stmt]`

## Functions

`_slot_body_is_empty`

1

`bool`

▼

Treat whitespace-only Data nodes the same as an empty slot body.

`def _slot_body_is_empty(slot_body: Sequence[object]) -> bool`

##### Parameters

Name
Type
Description

`slot_body`
`Sequence[object]`

##### Returns

`bool`
