All pages
Powered by GitBook
1 of 11

Lexical Elements

Loading...

Loading...

Literals

In BoxLang, literals are fixed values that are not variables and do not change. Here are the types of literals in BoxLang:

  1. String literals: These are sequences of characters. In BoxLang, you can denote them using single or double quotes. For example, "Hello, World!" or 'Hello, World!'.

  2. Numeric literals: These are integer or floating-point numbers. For example, 123, 456.789.

  3. Boolean literals: These represent truth values and can be either true or false.

  4. Null literal: This represents a null value and is denoted by null.

  5. Array literals: These are denoted by square brackets [] and can contain a list of values. For example, [1, 2, 3].

  6. Struct literals: These are denoted by curly braces {} and can contain key-value pairs. For example, { "key1": "value1", "key2": "value2" }. An ordered (linked) struct literal can be accomplished using square braces in lieu of curly: [ "key1": "value1", "key2": "value2" ].

  7. Date/Time literals: These represent a specific point in time. Strings which contain parseable dates can be interpreted as date/time objects in certain contexts. For example, dateFormat( "2024-05-12", "MM/dd/yyyy" ) will be operated upon as a date object. Note, however, that member date/time functions are not immediately available on a string literal.

  8. Query literals: These are used to create a query object. They are represented in the format queryNew("column1,column2", "type1,type2", [ [ "data1", "data2" ] ]).

Remember, the way literals are used can vary depending on the context within the BoxLang code.

Comments

Lexical comments in BoxLang are an integral part of the language's syntax, serving as a tool for developers to annotate and explain their code. These comments are not processed by the BoxLang interpreter, meaning they do not affect the execution of the code, but they provide valuable context and explanation for human readers.

In BoxLang, there are two primary types of lexical comments: single-line comments and multi-line comments.

Single-line comments in BoxLang start with two forward slashes //. Everything following these slashes on the same line is considered part of the comment and is ignored by the BoxLang interpreter. For instance:

// This is a single-line comment in BoxLang

BoxLang Multi-line comments, on the other hand, are enclosed between /* and */. Everything within these symbols is considered part of the comment, regardless of how many lines it spans. For example:

/*
* This is a multi-line comment in BoxLang.
* It can span multiple lines.
*/

Lexical comments are not only for human readers. They can also be utilized by various tools to generate documentation, enforce coding standards, or even guide the behavior of the BoxLang interpreter in certain cases.

For instance, BoxLang supports a special type of comment known as a "hint". Hints are written as comments but are read by the BoxLang interpreter and used to provide additional information about the code. For example, you can use a hint to provide a description of a function:

/**
 * @hint This function calculates the sum of two numbers.
 */
function sum(a, b) {
    return a + b;
}

In this example, the @hint annotation in the comment provides a description of the sum function. This description can be read by tools that generate documentation, or by the BoxLang interpreter itself to provide more informative error messages.

Lexical comments in BoxLang also play a crucial role in code organization and readability. By providing clear, concise explanations of complex code blocks, developers can ensure that their code is easily understandable by others, including their future selves. This is particularly important in large projects or when working in a team, where understanding others' code quickly is often necessary.

Moreover, lexical comments can be used to temporarily disable sections of code during debugging or development. By commenting out a section of code, developers can isolate problem areas or prevent execution of certain code blocks without deleting them.

In conclusion, lexical comments in BoxLang are a powerful tool for improving the readability, maintainability, and usability of your code. They allow you to annotate your code with useful information, and can even influence the behavior of the BoxLang interpreter in some cases. Whether you're a beginner or an experienced BoxLang developer, understanding and utilizing comments effectively is a valuable skill.

Loading...

Semicolons

In BoxLang, semicolons may used to separate statements in script syntax. This is similar to their use in many other programming languages like JavaScript, Java, or C#. Statements on separate lines, however, do not require semicolons for separation.

A basic example using semicolons for termination on a single line:

var x = 10;
var y = 20;
var sum = x + y;

In this example, each line is a separate statement, and each statement is terminated with a semicolon.

Once again, semicolons are optional and may be omitted, especially when each statement is on a new line. If you want to put multiple statements on a single line, however, then semicolons become necessary to separate them:

var x = 10; var y = 20; var sum = x + y;

In this case, without the semicolons, the above code would fail to compile and would be syntactically incorrect.

Identifiers

Packages and Imports

In BoxLang, a package is a collection of related components, functions, and variables. Packages help in organizing code into logical units, making it easier to maintain and understand. They also provide a namespace that prevents naming conflicts between different parts of a program.

By default, the package space of a class is the directory path, in dot-notation, from the webroot. For example, the default package of a class located at models/foo/Bar.bx would be models.foo. This can, however, be defined in the class, separately from the path.

To define a package in BoxLang, you use the class keyword and define the package in the attributes of the class. For example:

class package="com.example" {
    // package code here
}

This code defines a package named com.example. All components and functions defined within this package will be part of the com.example namespace.

Importing in BoxLang is done using the import keyword. The import statement is used to bring in a package or a specific component from a package into the current scope. For example:

import "com.example.*";

This statement imports all components from the com.example package. If you want to import a specific component, you can do so like this:

import "com.example.MyComponent";

This statement imports only the MyComponent component from the com.example package.

It's important to note that BoxLang is a dynamic language, and as such, it resolves package and component names at runtime, not at compile time. This means that you can use dynamic strings in your import statements, like so:

import "com.example.#componentName#";

In this statement, #componentName# is a variable that contains the name of the component to import. This feature makes BoxLang very flexible, but it can also lead to runtime errors if the component or package doesn't exist.

Packages and imports in BoxLang provide a way to organize code into logical units and to reuse code across different parts of a program.

Java Interoperability

BoxLang is a powerful, optionally typed and dynamic language, with static-typing and static capabilities. It's designed to improve developer productivity thanks to a concise, familiar and easy to learn syntax.

One of the key strengths of BoxLang is its smooth Java interoperability. This makes BoxLang a natural choice for developers who are already familiar with the Java ecosystem. BoxLang can leverage Java's enterprise capabilities but also provides a more flexible and less verbose approach.

Java interoperability in BoxLang is achieved in several ways:

  1. Java Code Usage: BoxLang can use Java code seamlessly. All Java libraries, frameworks and classes can be used directly in BoxLang. The BoxLang compiler converts BoxLang code into Java bytecode, which can be executed on any machine that has a Java Virtual Machine (JVM). This means that BoxLang is fully compatible with Java and can interact with Java as if it were Java.

  2. Syntax: BoxLang's script syntax uses conventions from both Java and ECMA Script ( Javascript ). As such developers coming from both Java and Javascript, will find the language to be easy to learn, and powerful. BoxLang offers additional features, such as closures, builders, and dynamic typing, which can make the code more readable and expressive.

  3. Interoperability BoxLang's seamless interoperability with Java allows developers to enjoy the benefits of a modern, flexible language while still being able to leverage the robustness and enterprise capabilities of Java. This makes BoxLang a powerful tool for both Java development and scripting tasks.

BoxLang is a scripting language for web development that runs on the JVM, the .NET framework, and Google App Engine. It's known for its simplicity and powerful built-in features, making it a popular choice for rapid web application development. BoxLang offers excellent interoperability with Java, allowing developers to leverage the robustness and versatility of Java within their BoxLang applications.

Here are some ways BoxLang interoperates with Java:

  1. Java Objects: BoxLang can create and manipulate Java objects directly. This means you can instantiate Java classes, call methods, and access properties just like you would in Java, or import them directly in your classes. This allows you to use any Java library in your BoxLang code:

Imports:

class {
    import java:java.lang.String as stringObject;

    function formatErrorMessage( required string exceptionMessage, required Throwable error ){
        return stringObject.format( exceptionMessage, e.message );
    }

}

Class usage:

var stringObject = new java:java.lang.String;
stringObject.format( "An error occurred.  The message received was %s", e.message );

Direct Object Instantiation:

return new java:java.lang.String( a ).compareToIgnoreCase( b );
  1. Java Data Types: BoxLang can interact with Java's primitive and complex data types. For example, you can pass BoxLang arrays to Java methods that expect Java arrays, and BoxLang will automatically convert the data types for you.

  2. Java Exceptions: When you call Java methods from BoxLang, any exceptions thrown by the Java code can be caught and handled in BoxLang. This allows you to handle errors in a way that's consistent with the rest of your BoxLang code.

  3. Java Threads: BoxLang can create and manage Java threads, allowing you to write multithreaded applications in BoxLang using Java's robust threading model.

  4. Java Streams: BoxLang can work with Java's I/O streams, allowing you to read and write data in a way that's efficient and scalable.

BoxLang's interoperability with Java allows developers to leverage the power and versatility of Java within their BoxLang applications. Whether you're calling Java methods, using Java libraries, or interacting with Java EE services, BoxLang makes it easy to integrate with Java in ways that are simple and productive.

Loading...