Lasso Basics

Welcome to Lasso! This guide is meant to assist you in diving into the language. It assumes you have prior programming experience and that you have properly installed Lasso and configured Lasso to work with your web server. (See the appropriate installation instructions for your operating system in the Lasso Server Guide.)

The examples in this particular chapter can all be run inside the Lasso Quick Code area in the Lasso Server Admin web application. (Be sure to leave the “<?lasso” and “Auto-collect” checkboxes checked.)

Hello World

The obligatory “Hello World” example is extremely simple in Lasso:

'Hello, world.'
// => Hello, world.

As you can see, the code just declares the string 'Hello, world.', and that value is produced when you run the code.

Here is what’s going on behind the scenes: Lasso is an object-oriented language, so everything is an object. Every object has a member method named asString that it can implement. (Left unimplemented, it just returns the name of the object’s type.) Any statement that is just an object by itself or produces an object will have that object’s asString method implicitly called, and that value will therefore be produced. (For a counter-example, assignment statements don’t produce an object and so will not cause any value to be produced.)

In our example, we used a string literal to create a string object. Since that statement produces an object, Lasso then calls the string->asString method on that object which has been implemented to return the value of the string.

Captures

This implicit call of the asString method is important for code inside a capture. Captures form the backbone of Lasso: all Lasso code executes inside a capture. (See the Captures chapter for more information.) They can also be used for code within a conditional. However, not all captures output the values generated by calls to asString. In the example below, the values in the if statement won’t be displayed, but the values in the loop statement will be:

if(false) => {
   'will never get to me'
else(false)
   "will never get to me either"
else
   `got to me, but I won't be displayed`
}

loop(5) => {^
   loop_count + ', '
^}

// => 1, 2, 3, 4, 5,

Here we have a basic if statement using a normal capture ({ ... }) and a loop statement using an auto-collected capture ({^ ... ^}). The auto-collect capture concatenates all the values produced using asString and returns them while the normal capture just ignores them.

Variables

There are two different types of variables in Lasso: local variables and thread variables. Local variables are lexically scoped while thread variables are available anywhere in the execution of the thread they are defined in. Here is an example of creating and using both types of variables:

local(localVar) = 30    // Creates a local variable
var(threadVar)  = 12    // Creates a thread variable
$threadVar + #localVar

// => 42

One handy feature of local variables is decompositional assignment. This makes it easy to assign values from array-like types into locals:

local(a, b, c) = array('rhino', 'runs', 'rapidly')

#a
// => rhino
#b
// => runs
#c
// => rapidly

For more information on variables, see the Variables chapter.