Skip to content

JavaScript Static Types

brett hartshorn edited this page Jul 30, 2015 · 5 revisions

The JavaScript backend by default will inject code that checks the type of function arguments at runtime. The runtime type checking is applied to any statically typed arguments in a function.

def myfunction( a:int, b:float, c:string, d:MyClass ):
    pass

Above defines a function that will fail if not given the proper argument types, the first must be an integer, followed by: a float, a string, and a custom class called MyClass. A TypeError will be thrown if any of those arguments are not the correct type.

When you have tested your program, and ready for production release, you can disable the type checking using the --release option.

rusthon.py myinput.md myoutput.tar --release

Arrays

Typed arrays can be declared using syntax inspired by Golang, a type starting with [] is an array, and the type is given at the end. For example a 2D array of strings looks like this: [][]string.

def F( arr:[]int ):
    pass

The example above will check the first element of arr at runtime, and throw an error if it is not an integer.

Callbacks

The syntax for a callbacks is also inspired by Golang. The special keyword func followed by the arguments and return type are each enclosed in parenthesis.

def F( mycallback:func(int float)(string) ):
    pass

In the example above the function F must be given as the first argument a callback function that takes two arguments, the first is an int and the second is a float, and returns a string. note: the arguments are space separated.

Gotchas

  • the float type is valid for any number, because JavaScript always stores all numbers as floats, and a literal like 1.0 will be shortened to 1 when coerced to a string.
  • the int type checks if . appears in the number when converted to a string.
  • only the first element of a typed array is check to see if it is of the correct type
  • inside a webworker the static type syntax is used for runtime class restoration, and not for runtime type checking.

Examples

Sidebar

Clone this wiki locally