An assert library for AtScript based on tcomb type combinators.
Setup
npm install tcomb-ats
Add the following keys to your traceur options:
"types": true "typeAssertions": true "typeAssertionModule": "path/to/tcomb-ats" "annotations": true // if you also want annotations
Set "typeAssertions": false
in production.
Demo
git clone https://github.com/gcanti/tcomb-ats.git
cd tcomb-ats
npm install
gulp
Open ./example/index.html
in a browser.
Syntax
Primitives
var t = ;var Str = tStr; // stringsvar s: Str = 'a'; // okvar s: Str = null; // throws (tcomb's primitives are not nullable)var s: Str = 1; // throws // numbersvar Num = tNum;var n: Num = 1; // ok // booleansvar Bool = tBool;var b: Bool = true; // ok // you can use other irreducible types provided by tcomb// or define your own, see tcomb documentation
Nullable types
Use the maybe<T>
combinator:
var s: maybe<Str> = 'a'; // okvar s: maybe<Str> = null; // ok
Functions
: Num { return a + b;} ; // ok; // throws
Lists
Use the list<T>
combinator:
var list = tlist; var a: list<Str> = 'a'; // okvar b: list<Str> = 1; // throws // you can also use the `Array<T>` syntaxvar c: Array<Str> = 'a';
Classes
{ thisname = name; thissurname = surname; } var p1: Person = 'Giulio' 'Canti'; // okvar p2: Person = 'Giulio'; // throws
Tuples
Use the tuple
combinator:
var tuple = ttuple; var t: tuple<Str Num> = 1 'a'; // okvar t: tuple<Str Num> = 1 2; // throws
Dicts
Use the dict
combinator:
var dict = tdict; var d: dict<Str Num> = a: 1; // okvar d: dict<Str Num> = a: 'b'; // throws
Enums
Use the enums
combinator:
var Align = tenums; var e: Align = 'left'; // okvar e: Align = 'justify'; // throws
Subtypes
Use the subtype
combinator:
var Positive = t; var n: Positive = 1; // okvar n: Positive = -1; // throwsvar n: Positive = 'a'; // throws
Unions
Use the union
combinator:
var StrOrNum = t; var u: StrOrNum = 'a'; // okvar u: StrOrNum = 1; // okvar u: StrOrNum = true; // throws
tcomb's structs
var Person = t; var p: Person = name: 'Giulio' surname: 'Canti'; // okvar p: Person = 1; // throws
License
The MIT License (MIT)