KScript docs
EN

Types and validation

In XML

In XML, the same types are attributes and elements of <type>:

<type name="Username" with="handle" max="16" not="@admin, @root">
  <validate test="not $value.name->starts_with('_')" message="A username cannot start with _" />
</type>
<type name="Admin" with="User, Auditable">
  <field name="level" type="int" default="1" />
</type>
<let name="who" type="Username" value="@joan" />
<type name="Point">
  <construct>
    <field name="x" type="number" />
    <set name="doubled" value="$x * 2" />
  </construct>
  <field name="doubled" type="number" />
</type>
type Username with [handle] {
  max: 16
  not: [@admin, @root]
  validate: not $value.name->starts_with('_'), 'A username cannot start with _'
}
type Admin with [User, Auditable] {
  level: int = 1
}
$who: Username = @joan
type Point {
  construct({ x: number }) {
    .doubled = .x * 2
  }
  doubled: number
}

A <construct> holds the fields a caller may pass, then the statements to run. A private member has private="yes" on its <field> or <fn>. kscript convert turns either form into the other.