Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- TUA
- -- Tua is a programming language which adds optional static type checking to the Lua programming language. Programs can be written in Tua, typechecked by the Tua compiler, then compiled into Lua and run anywhere Lua scripts are used. The type checking features in Tua are powerful, but optional, allowing programmers to mix dynamic and static programming styles as they wish. Tua aims to be backwards compatible with existing Lua code. So far i've implemented type checking for primitives, unions, tables, functions (and their arguments and return values), and type inference when declaring variables. Here are some examples of the current syntax:
- -- Declaring dynamically typed local variables
- -- (same syntax and behaviour as ordinary lua):
- local a = 1
- local b = true
- local c = "hello"
- local d = {}
- local e = { hello = "world" }
- local function f( x, y )
- return x + y
- end
- -- Declaring statically typed local variables using explicit types:
- local a : number = 1
- local b : boolean = true
- local c : string = "hello"
- local d : table = {}
- local e : { string -> string } = { hello = "world" }
- local f : function( number, number ) -> number = function( x : number, y : number )
- return x + y
- end
- -- Declaring statically typed local variables using inferred types:
- auto a = 1
- auto b = true
- auto c = "hello"
- auto d = {}
- auto e = { hello = "world" }
- auto function f( x : number, y : number )
- return x + y
- end
- -- When variables are statically typed, the Tua compiler will emit error messages when they're incorrectly used. Here are some examples of incorrect usage and the corresponding error messages:
- a = "hello" -- Attempt to assign string to number
- b = nil -- Attempt to assign nil to boolean
- c["hello"] = 42 -- Attempt to index string value
- d() -- Attempt to call table value
- e.hello = false -- Attempt to assign boolean to string|nil
- f( 1, "2" ) -- Attempt to pass string as argument 2 to function( number, number ) -> number
- auto s = f( 1, 2 ); s = "hello" -- Attempt to assign string to number
- -- This project is a work in progress.
- -- If you have any feedback or suggestions to share with me, send them to my twitter: @DanTwoHundred, or dratcliffe@gmail.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement