nenu-lang / entity-methods
Stable

Entity Methods

  • Add of Entity after a function name to attach behavior to that receiver.
  • self is the current receiver and has the exact entity or child-entity type.
  • Built-in collection and primitive methods remain owned by their receiver types.

Attaching A Method

code
1bool fn isLarge of Expense {2  return self.amount >= 1003}45string fn displayTitle of Expense {6  return self.title.trim()7}

of Expense attaches the function to Expense. Inside the body, self is the current expense. Outside the declaration, call the method through an expense value.

Calling A Method

code
1if expense.isLarge() {2  review_required = true3}45label = expense.displayTitle()

The receiver before the dot chooses the method namespace. Nenu checks that the method exists for the resolved receiver type and that its parameters and return value match the call site.

Mutating Through Self

code
1fn recordSettlementTime(time settled_at) of Settlement {2  self.settled_at = settled_at3}

Child Entity Methods

code
1bool fn hasPositiveShare of Expense.shares {2  return self.amount > 03}

Use the exact dotted child type after of. The parent segment is PascalCase and the child segment is lower-camel, matching the child entity declaration. Define that relationship first on the Relationships And Children page.

Receiver-Specific Built-Ins

  • Arrays own methods such as filter, collect, and push.
  • Strings own methods such as trim and contains.
  • Numbers and time values own their documented primitive methods.
  • An entity does not inherit the methods of one of its fields.
  • A custom entity method may reuse a name owned by an unrelated receiver, but cannot replace a built-in on the same resolved receiver type.
code
1fn filter of Expense {2  return self3}

Common Mistakes

WrongCorrect
fn isLarge(Expense self)fn isLarge of Expense
Use thisUse self.
Assume mutation saves automaticallyCall save at the owning backend workflow boundary.
Attach to Expense.SharesUse Expense.shares.

Continue Learning