TypeDraft: Language is the New Framework

栏目: IT技术 · 发布时间: 5年前

内容简介:TypeDraft is a superset of TypeScript and adds DSL and Macro mechanism to the language. If TypeScript is JavaScript that scales, then TypeDraft intends to be TypeScript that scales.In this part, we will go through 3 DSLs: debug, match, and watch.The syntax

TypeDraft is a superset of TypeScript and adds DSL and Macro mechanism to the language. If TypeScript is JavaScript that scales, then TypeDraft intends to be TypeScript that scales.

DSL

In this part, we will go through 3 DSLs: debug, match, and watch.

The syntax of DSL is:

{
    "use <dsl name>";
    ...
}

Debug: Conditional Compilation

We can build different versions of a library by specifying different environment variables. For example, in Implement a DSL , we implemented a DSL named as “debug”:

let value = 1;{
    "use debug";
    console.log(`[Debug]: value is ${value}`);
}

The result of compilation depends on the NODE_ENV you provided in npm script:

"scripts": {
    "build": "cross-env NODE_ENV=production td ./src",
    "dev": "cross-env NODE_ENV=dev td ./src"
},

In dev mode, the output will be exactly:

let value = 1;
console.log(`[Debug]: value is ${value}`);

In production mode, everything in the context of “debug” will be removed:

let value = 1;

In this way, we can achieve the goal of conditional compilation. For example, if you have code for both dev and production:

let url: string = "";
{
    "use dev";
    url = ... // address in test environment 
}{
    "use production";
    url = ... // address in production environment
}

then you can generate two different versions of library without any runtime code.

Match: Simplify If-Else

We can simplify logic of if-else with “match”, if we have code such as:

let filtered;
let currentFilter = 'all';filtered = currentFilter === 'all' ? 
        items : currentFilter === 'completed' ? 
        items.filter(item => item.completed) : items.filter(item => !item.completed);

In DSL match, we will write it in this way:

let filtered;
let currentFilter = 'all';{
    "use match";
    (currentFilter: "all") => filtered = items;
    (currentFilter: "completed") => filtered = items.filter(item => item.completed);
    () => filtered = items.filter(item => !item.completed);
}

this example can be found in svelte-draft-todo-mvc , for svelte-draft, see Svelte with TypeScript .

Watch

We can write intutive code with “watch” to implement the traditional counter demo:

export default function App() {
    let count = 0;    function handleClick() {
        count += 1;
    }    let doubled: number;    {
        "use watch";
        doubled = count * 2;
    }    <button onClick={handleClick}>
        Clicked {count} {count === 1 ? 'time' : 'times'}
    </button>;
    <p>{count} doubled is {doubled}</p>}

you can find this example in svelte-draft-tutorial/2-reactivity/reactive-declarations/App.tsx .

Macro

The macro support in typedraft is primitive, but still useful. JSX is used to define and use a macro, for this part, see JSX as Macro .

Together with macro, typedraft supports literate programming paradigm.

Ray Tracing in Literate Programming is a good example because the site itself is generated from code, you can find the usage of both macro and svelte-draft.

For example, the skeleton of a section is sketched by:

export default function Section() {
    let canvas: HTMLCanvasElement;
    onMount(() => {        function generateColor(ray: Ray) {
            <GenerateColor />;
        };        <RenderImage />;
    });    let width = 600;
    let height = 300;    <div>
        <canvas bindRef={canvas} width={width} height={height}></canvas>
    </div>
}

Then, implementation of macro RenderImage and GenerateColor is placed in other places to keep the structure of code clear and easy to understand.

For more info about typedraft, see https://mistlog.github.io/typedraft-docs/docs/ . I’m sorry that this article is mainly example and link driven, natural language is so hard to deal with!


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

从问题到程序

从问题到程序

裘宗燕 / 机械工业出版社 / 2005-9-1 / 36.00元

本书以C作为讨论程序设计的语言,讨论了基本程序设计的各方面问题。书中给出程序实例时没有采用常见的提出问题,给出解答,再加些解释的简单三步形式,而是增加了许多问题的分析和讨论,以帮助读者认识程序设计过程的实质,理解从问题到程序的思考过程。书中还尽可能详尽地解释了许多与C语言和程序设计有关的问题。 本书适合作为高等院校计算机及相关专业的教材,也可供其他学习C程序设计语言的读者阅读。一起来看看 《从问题到程序》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

SHA 加密
SHA 加密

SHA 加密工具