Variables
Variables are commonly declared using the var keyword:
1
var num = 42
2
var str = "42"
3
var bool = true
Copied!

Lexical variables

These kinds of variables are lexical, but statically block scoped. This is the usual way of declaring variables in Sidef.
1
var x = 42 # sets the lexical x to 42
2
say x # prints the lexical value of x
Copied!

Static variables

This kind of variables are static, block-scoped and initialized only once.
1
static x = 42 # sets the static x to 42
2
say x # prints the static value of x
Copied!

Global variables

Global variables are declared at the top-level of the current namespace. They can be accessed from everywhere, anytime. However, try to avoid using them, unless you really don't have any better alternative.
1
global x = 42 # sets global x to 42
2
say x # prints the global value of x
Copied!

Local variables

Local variables (also known as "dynamically scoped variables") can be used to localize array/hash lvalues or global variables to a limited scope.
1
global x = 42 # sets the global x to 42
2
do {
3
local x = 100 # localizes x inside this block to 100
4
say x # prints the local value of x
5
}
6
say x # prints the global value of x (42)
Copied!
A slightly more advanced example, illustrating the localization of an hash lvalue, would be:
1
func foo(h) {
2
say h{:key}
3
}
4
5
var h = Hash(key => "a")
6
7
foo(h) # prints "a"
8
do {
9
local h{:key} = "b" # local change only
10
foo(h) # prints: "b"
11
}
12
foo(h) # prints: "a"
Copied!

Variable scoping

All variables (including functions and classes) are block scoped in the following way:
1
var x = 'o'
2
3
do {
4
say x # o
5
var x = 'm'
6
say x # m
7
do {
8
say x # m
9
var x = 'b'
10
say x # b
11
}
12
say x # m
13
}
14
15
say x # o
Copied!
Declaring multiple variables at once is also possible:
1
var (x, y, z) = (3.14, false, "foo")
Copied!
We can, also, declare variables with some default values:
1
var (x, y=755, z=777) = (666, 655)
2
3
say x # prints: 666
4
say y # prints: 655
5
say z # prints: 777
Copied!

Slurpy variables

Slurpy (or greedy) variables are a special type of variables which can be initialized with a list of values, creating automatically a container to hold the data.
1
var *arr = (1,2,3) # creates an Array
2
say arr # prints: [1,2,3]
3
4
var :hash = (a => 1, b => 2) # creates an Hash
5
say hash # prints: Hash(a=>1, b=>2)
Copied!

Working with variables

Any method applied to a variable is applied on the object at which the variable is pointing at:
1
var x = 'sidef'
2
say x.uc # prints: `SIDEF`
3
say x # prints: `sidef`
Copied!
Special ! at the end of a method changes the variable in-place (almost like in Ruby):
1
var x = 'sidef'
2
x.uc! # notice the `!`
3
say x # prints: `SIDEF`
Copied!
Appending the = sign at the end of arithmetic operators, the variable will be changed in place:
1
var x = 5
2
x += 10 # adds 10 to "x"
3
say x # prints: 15
Copied!
The special operator := (also available as \\=), assigns a value to a variable if the current value of the variable is nil:
1
var x = nil
2
x := 42 # assigns 42 to x if x is nil
3
x := 99 # x is already defined
4
say x #=> 42
Copied!
The defined-or operator \\ can be used for checking if a variable is defined or not:
1
var x = nil # nil represents an undefined value
2
say defined(x) # prints 'false'
3
4
x \\ say 'undefined' # prints 'undefined'
5
x \\= 99 # sets x to 99
6
x \\ say 'undefined' # no longer prints 'undefined'
Copied!

Special identitiers

    ARGV is an Array that contains the program's command-line arguments, that were not given to Sidef.
    ENV is an Hash copy of environment variables and their values when the program was started.
    ARGF is a FileHandle object used to read lines from argument-files or from STDIN when no argument has been specified.
    DATA is a FileHandle object that points to the data stored after the __END__ or __DATA__ tokens.
1
say ARGV # command-line arguments
2
say ENV{:HOME} # get an environment variable
3
4
ARGF.each { |line| # cat-like program
5
say line
6
}
7
8
DATA.each {|line| # iterate over lines in __DATA__
9
say line
10
}
11
12
__DATA__
13
hello
14
world
Copied!

Topic variable

The special topic variable (_) is declared at compile-time in each block-object in the program. You may not see its real name very often, because it has been overtaken by the elegant prefix dot (.) operator:
1
[25,36,49].map {.sqrt} \
2
.each{.log.say}
Copied!
...where .sqrt really means _.sqrt, and .log.say means _.log.say.
Last modified 1yr ago