-
Notifications
You must be signed in to change notification settings - Fork 51
JavaScript Static Types
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 ):
passAbove 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 --releaseTyped 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 ):
passThe example above will check the first element of arr at runtime, and throw an error if it is not an integer.
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) ):
passIn 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.
- the
floattype is valid for any number, because JavaScript always stores all numbers as floats, and a literal like1.0will be shortened to1when coerced to a string. - the
inttype 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