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!


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

查看所有标签

猜你喜欢:

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

互联网思想十讲

互联网思想十讲

段永朝 / 商务印书馆 / 2014-10 / 68.00

本书是作者为北京大学新闻与传播学院硕士生开设的《互联网前沿思想》课程的讲义。作者力图从技术、经济和社会的角度,在大尺度上观察互联网究竟根植于什么样的文化土壤。作者选择了复杂性、社会网络分析、公共空间这三个维度展开分析,为读者呈现出了脱胎于工业时代的互联网继承了哪些思想,并对哪些思想做出了彻底的颠覆。一起来看看 《互联网思想十讲》 这本书的介绍吧!

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

在线压缩/解压 HTML 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具