LEARN VOID / DATA & BEHAVIOR

Callable structs

Let a small object express an operation.

Complete program for the synchronous entry adapter. The result below is the returned integer.

callables.voidDownload source
value struct coupon:
    discount: i32

func (offer coupon) call(price: i32) -> i32:
    return price - offer.discount

func entry(price: i32) -> i32:
    var save_three = coupon(discount = 3)
    return save_three(price)
Input 12Expected result 9

Store the operation’s data

The coupon stores a discount of three. Its call receiver method subtracts that amount from a price. Use prices at least three for this simple arithmetic example; no currency or pricing library is implied.

Invoke the instance

save_three(price) invokes the compatible call method. Price 12 returns 9. The main example needs no generic helper, second callable type or lifecycle checks.

Keep ordinary type rules

The coupon is a value struct, so ordinary copies remain independent values. A managed callable instead retains shared identity. A generic function can invoke a concrete callable through the same call syntax when that specialization supports it. Visibility, readonly and receiver effects remain enforced.