language · keyword ·

invariant

Declare valid object state

complete-programcpufunctionskeywordyeho

Use contracts at important boundaries where callers and maintainers need one visible definition of correctness.

invariant

Status: experimental

Class invariant

Use contracts at important boundaries where callers and maintainers need one visible definition of correctness.

cpu · complete-program · type checked

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))
}
project.mech
package = "tests.compiler.errorsTasksEffects.classInvariant"
version = "0.1.0"
backend = "cpu-oracle"
osTarget = "windows"
archTarget = "x64"
selfContainedApp = false
Notes and source

Errors and contracts are experimental in the current conformance edition.

Keep the condition deterministic and cheap enough to be useful as evidence.

yeho/parser_declarations.cpp parseContractClauses and parseThingInvariant; conformance errors-contracts