How is EK9 structured compared to Pascal?

← Syntax and Structure Rules · Ref: Q907

EK9 source files have a Pascal-like structure with mandatory section headers. Like Pascal's type/var/procedure/function sections, EK9 uses 'defines class/record/function/program' sections.

FILE STRUCTURE (top to bottom)
1. #!ek9 — shebang line (required)
2. defines module — module declaration (like Pascal's unit/program name)
3. defines constant — constants (like Pascal's const section)
4. defines type — enumerations and constrained types (like Pascal's type section)
5. defines record — value types (like Pascal's record types)
6. defines function — standalone functions (like Pascal's function/procedure section)
7. defines trait — interfaces (no Pascal equivalent — like Java interfaces)
8. defines class — classes (like Pascal's object types)
9. defines component — DI-injectable components
10. defines program — entry points (like Pascal's program body)
11. //EOF — end marker (like Pascal's end.)

PASCAL COMPARISON

Pascal: EK9:

  unit MyUnit;                  defines module my.package
  type                          defines record
    TPoint = record               Point
      x: Real;                      x as Float: 0.0
      y: Real;                      y as Float: 0.0
    end;
  var                           defines constant
    MaxSize: Integer = 100;       MAX_SIZE <- 100
  procedure DoWork;             defines function
                                  doWork()
  begin ... end.                defines program
                                  Main()

KEY DIFFERENCES FROM PASCAL

- No semicolons or 'begin/end' — indentation defines scope
- No 'var' section — variables declared inline with <-
- Multiple sections of same type allowed (for grouping)
- Sections can appear in any order (except 'defines module' must be first)

See Q893 for section header details. See Q93 for class definitions.

Example

defines module qa.syntax.pascalstructure

  defines constant

    <?-
      Constants section — like Pascal's 'const' section.
    -?>
    MAX_RETRIES <- 3
    GREETING_PREFIX <- "Hello"

  defines record

    <?-
      Record section — like Pascal's 'type' with record types.
    -?>
    Dimensions
      widthMm as Float: 0.0
      heightMm as Float: 0.0

      Dimensions()
        ->
          widthMm as Float
          heightMm as Float
        this.widthMm :=: widthMm
        this.heightMm :=: heightMm

      default operator

  defines function

    <?-
      Function section — like Pascal's 'function'/'procedure'.
    -?>
    calculateArea() as pure
      -> dims as Dimensions
      <- rtn as Float: dims.widthMm * dims.heightMm

    formatGreeting() as pure
      -> recipientName as String
      <- rtn as String: `${GREETING_PREFIX}, ${recipientName}`

  defines class

    <?-
      Class section — like Pascal's 'object' type section.
    -?>
    RetryCounter
      attemptNumber as Integer: 0

      increment()
        if attemptNumber < MAX_RETRIES
          attemptNumber++

      hasRetriesLeft() as pure
        <- rtn as Boolean: attemptNumber < MAX_RETRIES

      getCurrentAttempt() as pure
        <- rtn as Integer: attemptNumber

      default operator

  defines program

    <?-
      Program section — like Pascal's main program body.
    -?>
    PascalStructureDemo()
      stdout <- Stdout()

      // Using constants
      stdout.println(formatGreeting("Steve"))

      // Using records
      boxSize <- Dimensions(200.0, 150.0)
      areaValue <- calculateArea(boxSize)
      stdout.println(`Box area: ${areaValue} sq mm`)

      // Using classes
      retries <- RetryCounter()
      retries.increment()
      retries.increment()
      stdout.println(`Attempt: ${retries.getCurrentAttempt()}, retries left: ${retries.hasRetriesLeft()}`)
Other ways to ask this
  • What is the Pascal-like structure of EK9?
  • How does EK9 code organization compare to other languages?
  • What is the module structure of EK9?
  • How do I organize an EK9 source file?

Coming from another language?

Pascal: unit name; type TMyType = ...; var x: Integer; procedure DoWork; begin end. EK9 follows the same pattern with 'defines' sections. Java: no sections, classes/interfaces at top level. Python: no sections, mix everything. Go: package name; type/func/var at top level. EK9: Pascal-like 'defines class/function/record/program' mandatory sections.

Keywords: file, section, structure, unit, layout, defines, organization, Pascal, module