Template expressions

Use the following syntax to embed a Gosu expression in String text:

${ EXPRESSION }

For example, suppose you want to display text with some calculation in the middle of the text:

var mycalc = 1 + 1
var myVariable = "One plus one equals " + mycalc + "."

Instead of this multiple-line code, embed the calculation directly in the String as a template:

var myVariable = "One plus one equals ${ 1 + 1 }."

If you print this variable, Gosu prints:

One plus one equals 2.

Gosu evaluates your template expression at run time. The expression can include variables or dynamic calculations that return a value:

var s1 = "One plus one equals ${ myVariable }."
var s2 = "The total is ${ myVariable.calculateMyTotal() }."

At compile time, Gosu uses the built-in type checking system to ensure the embedded expression is valid and type safe.

If the expression does not return a value of type String, Gosu attempts to coerce the result to the type String.

Alternative template expression syntax <%= ... %>

The syntax ${ EXPRESSION } is the preferred style for template expressions.

Gosu also provides an alternative template style. Use the three-character text <%= to begin the expression. Use the two-character text %> to end the expression. For example, you can rewrite the previous example as the following concise code:

var myVariable = "One plus one equals <%= 1 + 1 %>."