NexJ Logo

Using Scheme

One of the languages used for scripting is Scheme.  You can generate a Scheme Reference document by using the Generate NexJDoc tool in NexJ Studio.

Introduction to Scheme

NexJ Studio provides strong support for Scheme scripting, including:

  • interactive consoles
  • error detection and warnings
  • code completion (Ctrl+Space)
  • type hinting (Ctrl+1)
  • automatic formatting (Ctrl+Shift+F)
  • help tooltips
  • local and server step debugging
  • code highlighting

Scheme is NexJ's scripting language. Anyone with some programming experience in other high-level languages will find familiar concepts for variable assignment, conditional branching, function declaration, and so on. It supports operations on structured data such as strings, lists, and vectors, as well as operations on more traditional data such as numbers and characters. Use it in business rules, calculated attributes, validation, event handling, client scripting, and more.

Scheme is easily recognized when compared with procedural languages, such as C or Java.

SchemeC
(+ 1 2 3)
(+ (* 2 3) (* 4 5))
(< low medium high)
(foo x y)
(define (sq x) (* x x))
(1 + 2 + 3)
((2 * 3) + (4 * 5))
((low < medium) && (medium < high))
foo(x, y)
int sq(int x) { return (x * x) }

Notice the following about these Scheme statements:

  • They start with parentheses.
  • They are in prefix notation.
  • Many Scheme functions, such as addition (+), operate on a list of numbers.

Consider the following statement

(+ 1 2 3)

The parentheses go hand-in-hand with the prefix notation. The first item in the list after the opening bracket is the function. The add (+) function can take one or more arguments, like many of the basic math functions in Scheme. For example, delete (-), multiply (*), or divide (/).

As you can see, calling a function in Scheme is very similar to calling a function in languages such as Python or Java. Scheme code looks like this:

(function arg1 arg2 arg3 ... argn)

In Python or C, the equivalent code would look like this:

function(arg1, arg2, arg3, ... , argn)

The small difference being the function name is in parentheses and the arguments are space-delimited rather than comma-delimited.

NexJ Scheme is based on Scheme R6RS and extends to include concepts and behaviors from your model automatically as you add them. Your class attributes, events, and library functions all are treated just like all the core Scheme features.

One of the best ways to see how Scheme works is to type commands into the NexJ Studio Server Console and see the results. Hover over a function name to view the documentation for that function. To trigger Content Assist for Scheme, type Ctrl+Space. You will be presented with available variables and functions. 

Content Assist for Scheme

Scheme Content Assist allows developers to view the list of available variables and functions that can be invoked within a given context. To trigger Content Assist, press Ctrl+Space while the scripting pane is in focus or type the name of the variable followed by an apostrophe, for example this'.

By default, Content Assist displays the list of available variables and globally defined functions. For example, within a UI Action definition there are special variables like source, name, and parameter, which are defined within that context. In class definitions, the "this" variable is defined within any event or attribute definition.

Content Assist also attempts to list the available member variables of an object if its type can be detected.

For example, if a variable is the result of a read on the Entity class, then it is considered an Entity type.

(define person (read-instance Entity '() '(= (@ lastName) "Lamont") '()))

Typing person' automatically triggers Content Assist, which lists the available attributes and events that can be invoked on an Entity.

When defining a function, the arguments and return types can be declared in the function comments. The general form for declaring argument and return types is:

; @arg <argument name> <argument type> <description of argument>
; @ret <return type> <description of the returned object>

The following example shows the declaration of argument type for entity and return type for the function.

; @arg entity Person A person to perform an operation on 
; @ret Telcom The default telcom of the person 
(define (getTelcom entity)
   (logger'info (entity'firstName))
   ;return 
   (entity'defaultTelcom) 
)

In this case, typing entity' within the scope of the function definition triggers Content Assist for the entity variable. Since entity is declared as a Person type using the @arg tag, Content Assist lists the available attributes and events for the Person class.

If you assign the result of the above function to a variable, that variable is assumed to take on the type declared in the @ret tag.

(define telcom (getTelcom timLamont))

In this case, typing telcom' lists attributes and events declared in the Telcom class, because of the @ret tag used in the getTelcom definition.

External Scheme resources and tutorials

This list contains some additional Scheme resources for further reading.