Variables
In Seal, there are two types of variables as you would expect: Locals and Globals.
This language uses different syntax than other languages. Let's go through cases.
Naming
Variable naming must follow these rules:
-
Starting with
[a-zA-Z]or_ -
Can include
[a-zA-Z]or[0-9]or_
Locals
Any identifier you type is local.
a = 0 // local
PI = 3.14159 // local
Scope
define fun()
if false // this branch is never executed
a = 10
print(a) // null
fun()
Seal has function-scoping. That means any variable defined inside function is available all across the function life-span. Main scope is also a function. All variables are null-initialized when a function call happens. That is why a got printed as null.
Globals
i_am_local = 0 // this can be accessed only in this scope
$i_am_global = 1 // this can be accessed anywhere
print("hello, world") // since print is not found in locals,
// it is searched in globals regardless
// same as $print("hello, world")
If you put $ symbol in front of any identifier, it becomes global.
Variable Search Fallback
If symbol name is found in current scope, it is used.
a = 0
print(a) // 'a' is found locally
If symbol is not found locally, it fallbacks to global map and searches that symbol there.
print(some_variable_name) // ERROR: 'some_variable_name is not defined'
If you put $, it does not search local names and jumps to global map immediately.
a = "local"
$a = "global"
print($a) // does not print local 'a', instead searches global 'a'
This is how Seal prevents you from typing $print every time if you want to print something unless you define a local variable called print in that scope. Because print is just a global function. God, just imagine how annoying it would be...
Preventing Error in Lookups
If you just want to know whether a variable is defined globally without crashing your entire program, there is a special syntax for that.
Let me introduce you ? operator. Instead of crashing your program this returns null which means no value.
some_var? // is it defined?
name = $var? or "John" // if var doesn't exist or is null, use "John"
More about ? operator can be found here.