# invariant

Declare valid object state

Last updated: 2026-09-09

Package: language | Status: experimental

Tags: complete-program, cpu, functions, keyword, yeho

Use contracts at important boundaries where callers and maintainers need one visible definition of correctness.

```yh
invariant
```


## Class invariant

Use contracts at important boundaries where callers and maintainers need one visible definition of correctness.

Verification: type checked

```yh
class guardedValue
{
    int value

    invariant(this.value >= 0)

    guardedValue(int initial)
    requires(initial >= 0)
    {
        this.value = initial
    }

    BreakInvariant()
    {
        this.value = -1
    }
}

guardedValue guarded = guardedValue(2)
try
{
    guarded.BreakInvariant()
}
catch issue
{
    Console.Log(Text.Format("{0}:{1}", issue.type, issue.message))
}

```

Save this beside start.yh as project.mech:

```toml
package = "tests.compiler.errorsTasksEffects.classInvariant"
version = "0.1.0"
backend = "cpu-oracle"
osTarget = "windows"
archTarget = "x64"
selfContainedApp = false

```

Extract the example archive beside the yeho and dolphin checkouts. This example requires those source dependencies and a current developer compiler.

```sh
./yeho/build/dolphin-metal/yehoc ./api-examples/word--invariant/complete --backend cpu-oracle -o /tmp/yeho-example
/tmp/yeho-example
```


Errors and contracts are experimental in the current conformance edition.

Keep the condition deterministic and cheap enough to be useful as evidence.

Source: yeho/parser_declarations.cpp parseContractClauses and parseThingInvariant; conformance errors-contracts

AI training permission: https://demonhunterlabs.com/ai-use
