{"id":"word--invariant","name":"invariant","owner":"yeho","package":"language","kind":"keyword","signature":"invariant","description":"Declare valid object state","context":"Use contracts at important boundaries where callers and maintainers need one visible definition of correctness.","parameters":[],"returns":"","members":[],"tags":["complete-program","cpu","functions","keyword","yeho"],"updated":"2026-09-09","source":{"repository":"yeho","path":"parser_declarations.cpp parseContractClauses and parseThingInvariant; conformance errors-contracts"},"status":"experimental","notes":["Errors and contracts are experimental in the current conformance edition.","Keep the condition deterministic and cheap enough to be useful as evidence."],"examples":[{"id":"complete","title":"Class invariant","description":"Use contracts at important boundaries where callers and maintainers need one visible definition of correctness.","code":"class guardedValue\n{\n    int value\n\n    invariant(this.value >= 0)\n\n    guardedValue(int initial)\n    requires(initial >= 0)\n    {\n        this.value = initial\n    }\n\n    BreakInvariant()\n    {\n        this.value = -1\n    }\n}\n\nguardedValue guarded = guardedValue(2)\ntry\n{\n    guarded.BreakInvariant()\n}\ncatch issue\n{\n    Console.Log(Text.Format(\"{0}:{1}\", issue.type, issue.message))\n}\n","manifest":"package = \"tests.compiler.errorsTasksEffects.classInvariant\"\nversion = \"0.1.0\"\nbackend = \"cpu-oracle\"\nosTarget = \"windows\"\narchTarget = \"x64\"\nselfContainedApp = false\n","tags":["cpu","complete-program"],"verification":"type checked","run":"cpu-oracle","source":"tests/compiler/errors-tasks-effects/class-invariant/start.yh","sha256":"62398b145e610841dbb819cbdda68d88078bcd57c884834e21128558fe98b31b","checked":"2026-09-09","diagnostic":"","observedOutput":null,"checkedBackend":null}],"searchTerms":[],"url":"https://demonhunterlabs.com/academy/api?item=word--invariant","json":"https://demonhunterlabs.com/reference/items/word--invariant.json","markdown":"https://demonhunterlabs.com/reference/text/word--invariant.md"}
