KScript docs
EN

Types and validation

Values without quotes

Some values need no quotes, the same way a regex needs none in JavaScript.

  • Durations are literals anywhere: 30s, 500ms, 1h30m. See lengths of time.
  • Emails, URLs, UUIDs and regexes are literals anywhere: joan@example.com, https://example.com/docs, 3f2a8c1e-9b7d-4c2a-8e1f-0a9b8c7d6e5f, /^[a-z]+$/i. The editor checks them as you type. A URL starts with http://, https://, ftp://, ws:// or wss://, so its // is never a comment, and it ends at a space, a comma, a bracket or ->.
  • Slash paths are literals anywhere when each name is letters, digits, _, -, ., ~ or %: /docs/getting-started, /v1/users/12. A slash path that is also a regex, such as /blog/ or /a/i, is a regex where no type is declared, and a slash path where one is. A path with spaces in its names, such as /My Apps/Notes, needs quotes, except as the whole value of a declared slash_path ($menu: slash_path = /My Apps/Notes). $name and { expression } inside one are filled in.
  • File paths are literals anywhere when they start ./, ../ or ~/: ./data/input.txt, ../shared/rates.json, ~/notes.md. Their names follow a slash path's rules, and $name and { expression } inside one are filled in: ./days/day{ $n }.txt. A path that is absolute or has spaces in it is quoted: '/var/log/app.log'.
  • Handles are written as @joan only where a handle is expected: a handle variable or parameter, a not: or one_of: list on a handle type, or next to a handle in ==. Everywhere else, @joan still names the function joan.
  • Words need no quotes where the type is read from text. That covers an enum ($color: Color = red), a type with a pattern ($size: Size = 1920x1080), a refinement of text ($slug: Slug = my-post) and the native types: domain = example.com, ip = 10.0.0.1, cidr = 10.0.0.0/8, timezone = Europe/London, semver = 1.4.2, duration = 1h30m, color = red, country = GB and the rest. Method and function arguments of those types work the same way: $brand->contrast_ratio(white), @allow(10.0.0.0/8), @release(1.4.2-beta.1).

Quoted text always works too: $who: handle = '@joan'.

<enum name="Color" values="red, green, blue" />

<fn name="paint" params="color: Color, by: handle" value="'Painted ' + $color + ' by ' + $by" />

<let name="me" type="handle" value="@joan" />
<echo value="@paint(green, @joan)" />
<echo>{ $me == @joan }</echo>
enum Color { red, green, blue }

fn paint(color: Color, by: handle) => 'Painted ' + $color + ' by ' + $by

$me: handle = @joan
echo @paint(green, @joan)
echo "{ $me == @joan }"

Recognizing a custom literal by its pattern

A visible refinement of string, or of a native type written as a word (domain, semver, timezone and the like), can give an unquoted word its type. Declare a pattern on the refinement itself. The pattern must match the whole word, with its usual regex flags. A word can join names and numbers with -, +, / or ., with no spaces: $post = my-first-post, $host = api.corp.com, $v = 1.4.2. Numbers alone are still arithmetic (1+2 is 3), and when no type fits the whole word, no part of it is read as a type on its own.

<type name="PasskeySessionKey" with="string" min="8" pattern="/^passkey_[a-zA-Z]+$/">
  <normalize value="$value->trim()" />
</type>

<let name="key" value="passkey_abc" />
<echo value="$key" /> <!-- passkey_abc -->
type PasskeySessionKey with [string] {
  min: 8
  normalize: $value->trim()
  pattern: /^passkey_[a-zA-Z]+$/
}

$key = passkey_abc
echo $key  // passkey_abc

KScript selects the type before running its normalization and validation. If several types match the word, the checker and runtime name all the candidates. Choose one with $key = passkey_abc as PasskeySessionKey or a typed assignment such as $key: PasskeySessionKey = passkey_abc. The checker also warns when visible types declare identical regexes with the same flags. Recognized names, flags, and enum arguments keep their existing meanings.

Quotes keep text as a string. To check a computed value, write $key = "passkey_{$kind}" as PasskeySessionKey. A repeated word reuses its type lookup while the same declarations are visible. A computed cast evaluates its input on every call. For a short repeated string, KScript can reuse a successful conversion governed entirely by static rules. With a custom normalizer or validator, it runs that code again and can reuse a successful regex match on the resulting text.

The same double-quoted expression works on the right of a typed assignment. When a refinement accepts bare text, it reads braces and variable names literally.

Edit this page on GitHub