Variable Scopes
They gotta exist somewhere!
Last updated
Was this helpful?
They gotta exist somewhere!
Last updated
Was this helpful?
In the BoxLang language, many persistence and visibility scopes exist for variables to be placed in. These are differentiated by context: in a class, function, tag module, thread, or template. These scopes are Java maps but enhanced to provide case-insensitivity, member functions and more.
This idea that the variables you declare in templates, classes, and functions are stored in a structure makes your code highly flexible since you can interact with the entire scope fluently and with many different BIFs available. You can also bind them to function calls, attributes, etc. Thus, it capitulates on BoxLang being a dynamic language.
The only language keyword that can be used to tell the variable to store in a function's local scope is called var
. However, you can also just use the local
scope directly or none at all. However, we do recommend being explicit on some occasions to avoid ambiguity.
It's a good idea to scope variables to avoid scope lookups, which could in turn create issues or even leaks.
All scripts and templates have the following scopes available to them. Please note that the variables
scope can also be implicit. You don't have to declare it.
variables
- The default or implicit scope to which all variables are assigned.
All classes in BoxLang follow Object-oriented patterns and have the following scopes available. Another critical aspect of classes is that all declared functions will be placed in a visibility scope.
variables
- Private scope, visible internally to the class only
this
- Public scope, visible from the outside world
static
- Store variables in the classes blueprint and not the instance
super
- Only available if you use inheritance
Depending on the function's visibility, BoxLang places a pointer for the function in the different scopes below. Why? Because BoxLang is a dynamic language. Meaning at runtime, you can add/remove/modify functions if you want to.
Private
Function
variables
Public or Remote
Functions
variables
this
All user-defined functions declared in templates or scripts will have the following scopes available:
variables
- Has access to private variables within a Class or Page
local
- Function-scoped variables only exist within the function execution. Referred to as var
scoping. The default assignment scope in a function.
arguments
- Incoming variables to a function
All closures are context-aware. Meaning they know about their birthplace and surrounding scopes. If closures are used in side classes, then they will have access to the class' variables
and this
scope.
variables
- Has access to private variables from where they were created (Classes, scripts or templates)
this
- Has access to public variables from where they were created (Classes)
local
- Function-scoped variables only exist within the function execution. Referred to as var
scoping. The default assignment scope in a function.
arguments
- Incoming variables to a function
Lambdas are pure functions in BoxLang, they do not carry their surrounding or birth context. They are simpler and faster.
local
- Function-scoped variables only exist within the function execution. Referred to as var
scoping. The default assignment scope in a function.
arguments
- Incoming variables to a function
The following will throw an exception as it will try to reach outside of itself:
In BoxLang, you can extend the language and create your own custom components that can be used in script or templating syntaxes. They allow you to encapsulate behavior that can wrap up anything.
attributes
- Incoming component attributes
variables
- The default scope for variable assignments inside the component
caller
- Used within a custom component to set or read variables within the template that called it.
Here is the component:
We highly discourage peeking out of your component using the caller
scope, but it can sometimes be beneficial. Use with caution.
When you create threads with the thread
component, you will have these scopes:
attributes
- Passed variables via a thread
thread
- A thread-specific scope that can be used for storage and retrieval. Only the owning thread can write to this scope. All other threads and the main thread can read the variables.
local
- Variables local to the thread context
variables
- The surrounding declared template or class variables
scope
this
- The surrounding declared class scope
If you use a variable name without a scope prefix, BoxLang checks the scopes in the following order to find the variable:
Local (function-local, UDFs and Classes only)
Arguments
Thread local (inside threads only)
Query (not a true scope; variables in query loops)
Thread
Variables
CGI
CFFILE
URL
Form
Cookie
Client
IMPORTANT: Because BoxLang must search for variables when you do not specify the scope, you can improve performance by specifying the scope for all variables. It can also help you avoid nasty lookups or unexpected results.
Can be used in any context, used for persisting variables for a period of time.
session
- stored in server RAM or external storages tracked by unique web visitor
client
- stored in cookies, databases, or external storages (simple values only)
application
- stored in server RAM or external storage tracked by the running BoxLang application
cookie
- stored in a visitor's browser
server
- stored in server RAM for ANY application for that BoxLang instance
request
- stored in RAM for a specific user request ONLY
cgi
- read only scope provided by the servlet container and BoxLang
form
- Variables submitted via HTTP posts
URL
- Variables incoming via HTTP GET operations or the incoming URL