-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtypes.ts
More file actions
32 lines (26 loc) · 930 Bytes
/
Copy pathtypes.ts
File metadata and controls
32 lines (26 loc) · 930 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// SPDX-FileCopyrightText: 2026 MesTTo
//
// SPDX-License-Identifier: MIT
// Optional typing: declare types with `:` and function types with `->`. The type checker rejects
// ill-typed applications, and get-type / metatypes let a program inspect itself.
//
// Run it (after `pnpm build`): npx tsx examples/types.ts
import { runProgram, format } from "@mettascript/core";
const results = runProgram(`
; a data type and two constructors
(: Color Type)
(: Red Color)
(: Green Color)
; a typed function
(: favorite (-> Color Color))
(= (favorite Red) Green)
!(favorite Red) ; Green
; get-type reports the inferred type
!(get-type Red) ; Color
!(get-type favorite) ; (-> Color Color)
; an ill-typed application does not type-check (returns a type error)
!(favorite 42)
`);
for (const { query, results: rs } of results) {
console.log(format(query), "=>", rs.map(format));
}