Type level programming is a technique that exploits type system to represents information and logic, to the extent of language’s limit.
Since values are encoded by variable’s type, type level drives compiler to validate logic or even determine program’s output.
All validation and computation are conducted statically at compiling phase, so the greatest benefit of type-level programming is its safety and reliability.
As a rule of thumb, the more dynamic code is, the more flexible it can computes. Type level programming require all logic encoded into the source code. It is to hard to cram all logic into type system, as handling whimsical input from external source is either impossible or reduce source code to unwieldy state machine. So the niche of type level programming is usually encoding, pickling or parsing.
require: '^parentDirective' to express the constraints and enable directive communication. If the component is programmatically constructed, using type annotation is a natural way to express the constraints. (As in Angular 2.0,
query<ChildDirective>). We can go further in a language with full-bloomed type system.
The code above is just a demo. All html elements has type that denotes its structure. And one can tell whether a tag is in a html elemnt by calling
jQ(ele).has[Tag]. (Note: ele is value level variable and Tag is a type level constructor.) And inline element cannot contains block element, because inline element’s child must be a subtype of
Programmatical markup has several benefits:
- no switching between script and template
- static type checking
- component dependency requirements
- component communication
- subtyping, inheritance… Classical OOP features
- relatively clean layout (though not as concise as Jade/Slim)
The biggest problem is, well, type level templates is strongly constrained by host language. Dynamic languages simply cannot have it. Users of classical static typed language without type inference cannot afford the verbosity of deeply nested type. And languages capable of type level have different approaches and implementation towards Type Level Programming.
After all, type level is too crazy…, at least for daily business logic.