LiveScript
Paradigm | multi-paradigm, functional, object-oriented | ||
---|---|---|---|
Designed by | Jeremy Ashkenas, Satoshi Murakami, George Zahariev | ||
Developer | Jeremy Ashkenas, Satoshi Murakami, George Zahariev | ||
First appeared | 2011 | ||
LiveScript 1.3.1 / 22 October 2014 | |||
dynamic, weak, strong | |||
OS | Cross-platform | ||
License | MIT | ||
.ls | |||
Website |
livescript | ||
|
LiveScript is a functional language that compiles to JavaScript.
Syntax
LiveScript is an indirect descendant of and is partly compatible with Coffeescript.[1] The following is a fully Coffeescript-compatible hello-world example of LiveScript syntax.
hello = -> console.log 'hello, world!'
While calling a function can be done with empty parens, hello()
, LiveScript treats the exclamation mark as a single-character shorthand for function calls with zero arguments: hello!
LiveScript introduces a number of other incompatible idioms:
Name mangling
At compile time, the LiveScript parser implicitly converts dashed variable- and function names to camelcase.
hello-world = -> console.log 'Hello, World!'
With this definition, both the following calls are valid. However, calling using the same dashed syntax is recommended.
hello-world! helloWorld!
This does not preclude developers from using camelcase explicitly or using snakecase. Dashed naming is however, common in idiomatic LiveScript[2]
Pipes
Like a number of other functional programming languages such as F# and Elixir, LiveScript supports the pipe operator, |>
which passes the result of the expression on the left of the operator as the first argument to the expression on the right of it.
hello! |> capitalize |> console.log # > Hello, World!
Operators as functions
When parenthesized, operators such as not
or +
can be included in pipelines or called as if they were functions.
111 |> (+) 222 # > 333 (+) 1 2 # > 3
Typing
By default, LiveScript shares the weak, dynamic typing of Coffee- and JavaScript. However, the LiveScript compiler provides optional strong typing through the --const
flag.
num = 1 fun = (non-string) -> non-string = non-string.to-string! fun num
While perfectly permissible by default, when the --const
flag is used, the above will cause a compiler error of: [SyntaxError: redeclaration of constant "num" on line 4]
.
This happens because the --const
option simply treats all values as if they were declared as constants, at compile time, without using the not widely supported const
keyword in the output JavaScript.