Tuesday, October 2, 2012

Typescript for Python?

The new Typescript seems absolutely delicious!

I’ve been wondering how soon something similar will turn up for Python.  Whereas Typescript compiles to Javascript, a Python Typescript could just run as-is because…

… Python 3 now has argument and function annotation expressions.

Imagine annotations like:

def func(x: int, y: Thing) -> float:

both self-documenting and checkable!

Rather than using decorators, you might use the Python debugger hooks sys.settrace() to do checking at runtime, unless the interpreter is invoked in optimise mode.

I imagine you could override the built-in int function and so on such that __or__ and such work:

def get(i: int or string) -> object or None:

(Although I haven’t explored how that would affect instanceof(x,int))

I have my own Python JSON validation expressed as Python code already, which is equivalent to the declaration of duck-typing objects parameters; I’ll publish it as a stand-alone post soon.

There is active work on Python static typing too, which can be useful if a library arrives that lets you examine functions.  Imagine a light-table or Python text editor that did optional and inferred static typing as you typed…

Notes

  1. c0d3 reblogged this from williamedwardscoder
  2. williamedwardscoder posted this

 ↓ click the "share" button below!