Syntax & Semantics
Discover the BoxLang language
Last updated
Discover the BoxLang language
Last updated
BoxLang is a dynamic language that is fluent with a low verbosity syntax. It will feel like a very lean Java syntax. Check out our quick style guide if you are a Java/Kotlin/Python/PHP/Ruby/CFML/Rust developer, this will give you a first-hand look at the major semantics of the language.
BoxLang can be written in either templates, scripts, or classes. You will write one or more instructions in a file (.bx
, .bxs
, .bxm
), then run the file through a BoxLang engine or Command Line Interpreter like our REPL.
bx
- A BoxLang class
bxs
- BoxLang scripting file, for a-la-carte scripting
bxm
- BoxLang markup file, tag syntax is the default and used for creating rich views and templating
BoxLang also gives you a pre-set of defined headless components and functions available to you in any file in which you write your code. These components and functions allow you to extend the typical language constructs with many modern capabilities, from database interaction to PDF generation.
Also, note that you can use all the BoxLang types naturally with no imports:
Structs - (Maps)
Arrays - (Starting with an index of 1, not 0)
Strings
Dates
Numerics (Floats, shorts, integers, longs, etc)
XML
Queries
Functions
Tip: Please note that the BoxLang built-in functions (BIFs) are also first-class functions so that they can be passed around as arguments to other functions or closures or saved as variables.
Let's start by exploring some behavior in these types of files. Ensure you are using the BoxLang IDE Write the code; remember you can execute any BoxLang file within your editor by right-clicking the file and saying "BoxLang: Run File"
Let's write a simple scripting file that declares and uses a few boxlang types, and then you can print them on the console. You can run the script via your REPL or via https://try.boxlang.io
Now you can run it in the REPL: boxlang myscript.bxs
I can also use the templating language and build this in an HTML-enabled application.
If you want to see HTML being produced, then you will need to run the file in our MiniServer or CommandBox server. If not, just run the templating file via the REPL boxlang
binary.
Run this in the MiniServer or CommandBox or the REPL tool: boxlang myprogram.bxm
. You can also leverage scripting in templates by using the <bx:script>
template:
Please note that no types or visibility scopes you might be used to are present. BoxLang can also infer variable types on more distinct variables like dates, booleans, or numbers. It also can include a main()
method that can be invoked for you if you run the class via our REPL tool:
However, please note that you can fully leverage types if you like:
By default, the return type of every function and/or argument is any. Thus, it can be determined at runtime as a dynamic variable.
Please note that semi-colons are used to demarcate line endings in BoxLang ;
. They can be optional, however.
As we now live in a world of polyglot developers, we have added references below to other languages to see the differences and similarities between BoxLang and other major languages in usage today. Please note that this section is merely academic and to help developers from other language backgrounds to understand the intricacies of the BoxLang syntax.