Heroes / Examples / 09-holes

examples/gallery/09-holes.hero

Mark unfinished code with ??? and see which type the compiler expects.

The compiler reports the expected type and the names available at that point in the program. It checks the rest of the file as usual, but does not create an executable until you fill in the missing code. An example with unfinished code must be completed before it can run.

Run it

$ heroes run examples/gallery/09-holes.hero

These files introduce the language one feature at a time. This page shows the program and not a result, because no test compares their output byte for byte yet.

The program

## Working skeleton-first
#
# `???` is a valid expression anywhere and it is not an error: the compiler
# says what belongs there — the type it expects, and what is in scope. A
# file with holes checks everything else and produces no binary, so a
# half-written program is still a program the compiler will talk about.

record Entry
    name: str
    calories: i64

# Written, tested, done.
function total_calories(entries: [Entry]) -> i64
    total: i64 @= 0

    for e in entries
        total @ total + e.calories

    return total

# Next up: the compiler will name the expected type and list `entries`,
# `total_calories` and the fields of `Entry` when it reaches this.
function heaviest(entries: [Entry]) -> Entry
    ???

# The hole can be one argument of an otherwise finished line.
function summary_of(entries: [Entry]) -> str
    return "total " + total_calories(entries).to_str() + ", heaviest " + ???

test "the part that exists, works"
    lunch = [Entry(name: "soup", calories: 120), Entry(name: "bread", calories: 250)]
    assert total_calories(lunch) == 370

function main()
    print(summary_of([Entry(name: "tea", calories: 2)]))
examples/gallery/09-holes.hero, 35 lines

fingerprint sha256 6218c0df881a · 1 file · index.json

Next: 10-maps. All the examples. ← Previous: 08-ffi

Run one yourself: the four commands. Learn the language, or read the full specification in one file.