Skip to content

feat: function pre/post conditions #198

Description

@ganehag

Problem

Gengo has predicates on types (checked at construction), but no way to express constraints at the function boundary. Ada's with Pre => and with Post => aspects allow contracts that the type alone cannot express.

func divide(a int, b int) int
    pre  b != 0
    post result == a div b
{
    return a div b
}

Use cases

  • Preconditions on inputs that the type alone cannot enforce (e.g. "second argument must not be zero", "array must be non-empty")
  • Postconditions for documentation and debugging (checked in debug builds, elided in release)
  • Richer error messages than a runtime panic: "precondition violated: b must not be zero"

Design questions

  • Checked always, or only in a debug/assert build mode?
  • Can preconditions reference multiple parameters together (cross-parameter constraints)?
  • Syntax: pre/post as keywords, or requires/ensures, or aspect-style annotations?
  • Interaction with named return values: postconditions can reference named returns.
  • Should violation be a panic (like assert) or a typed error?

Needs design before implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Low priorityarea:compilerLexer, parser, compilerdesignLanguage or API design decision neededenhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions