Variables: Difference between revisions

4,020 bytes added ,  1 month ago
m
m (syntax highlighting fixup automation)
 
(10 intermediate revisions by 6 users not shown)
Line 1,810:
=={{header|EasyLang}}==
 
<syntaxhighlight lang="text"># it is statically typed
# it is statically typed
#
# global number variable
Line 1,818 ⟶ 1,819:
a[] = [ 2.1 3.14 3 ]
#
funcproc ffoo . .
# i is local, because it is first used in the function
for i range= 1 to len a[]
print a[i]
.
.
foo
call f
#
# string
domain$ = "easylang.onlinedev"
print domain$
#
# array of strings
fruits$[] = [ "apple" "banana" "orange" ]
print fruits$[]</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Eiffel}}==
Line 2,134 ⟶ 2,136:
Var ii = 6728 '' implicit integer
Var id = 6728.0 '' implicit double</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
void local fn Variables
dim a as long // dim var as type
dim as long b // dim as type var
long c // 'dim as' is optional
long x, y, z // declarate multiple vars in one line
CFStringRef s1 = @"Alpha", s2 = @"Bravo" // declare and assign in same statement
CFArrayRef array = @[s1,s2] // declare and assign an array
c = 123 // assign
CGRect r = {20,20,120,32} // declare and assign record (struct)
end fn
 
fn Variables
 
HandleEvents
</syntaxhighlight>
 
=={{header|GAP}}==
Line 2,368 ⟶ 2,388:
=={{header|J}}==
 
<syntaxhighlight lang="j">val=. 0</syntaxhighlight>
val=. 0
</syntaxhighlight>
 
J has two assignment operators. The =. operator declares, initializes, assigns, etc. a local variable. The =: operator does the same for a "global" variable.
 
<syntaxhighlight lang="j">fun =: 3 :0
fun =: 3 :0
val1 =: 0
val1 =. 2
Line 2,383 ⟶ 2,406:
0
val2
|value error</syntaxhighlight>
</syntaxhighlight>
 
Note that the language forbids assigning a "global" value in a context where the name has a local definition.
 
<syntaxhighlight lang="j">fun1 =: 3 :0
fun1 =: 3 :0
val3=. 0
val3=: 0
)
fun1''
|domain error</syntaxhighlight>
</syntaxhighlight>
 
But the purpose of this rule is to help people catch mistakes. If you have reason to do this, you can easily set up another execution context.
 
<syntaxhighlight lang="j">fun2 =: 3 :0
fun2 =: 3 :0
val4=. 0
3 :'val4=:y' y
)
fun2 ''</syntaxhighlight>
</syntaxhighlight>
 
Variables are referred to by name, and exist in locales (which may be used as classes, closures or other stateful references).
Line 4,595 ⟶ 4,623:
see Msg + nl
</syntaxhighlight>
 
=={{header|RPL}}==
A global variable is declared with the <code>STO</code> instruction after the variable name.
A local variable, which disappears at end of execution, is declared with the <code>→</code> instruction before the variable name, which is idiomatically in lowercase.
in both cases, the variable is initialized with the value at stack level 1, whatever its type.
123 'V' STO
123 → v
A new value can then be assigned with the <code>STO</code> instruction:
"AZ" 'V' STO
"AZ" 'v' STO
A variable can contain any type of data, which can change without need for reinitialization, as shown above.
It is possible to add, substract, multiple, divide... the content of stack level 1 to a variable with specific instructions, resp. <code>STO+, STO-, STO*, STO/</code>...
If the variable contains a list, STO+ will append or prepend the content of stack level 1, according to the order of the arguments:
456 'MYLIST' STO+ <span style="color:grey">@ prepend</span>
'MYLIST' 456 STO+ <span style="color:grey">@ append</span>
<code>STO-</code> and <code>STO/</code> have the same versatility:
123 'V' STO- <span style="color:grey">@ V ← 123 - V</span>
'V' 123 STO- <span style="color:grey">@ V ← V - 123</span>
 
=={{header|Ruby}}==
Line 4,628 ⟶ 4,674:
end
end</syntaxhighlight>
 
=={{header|Rust}}==
 
In Rust a variable needs to be declared before it is used. A variable is declared using the "let" command. Some common data types are: i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64, bool, char and String. Rust is strongly typed and strictly typed, however the Rust compiler can infer the data types based on how the variable is used. Therefore there are several different options for variable declaration:
<syntaxhighlight lang="rust">
let var01; // The compiler will infer the type or the compiler will ask for type annotations if the type can't be inferred
let var02: u32; // The compiler will check if a value is assigned to var02 before being used
let var03 = 5; // The compiler will infer the type or it will fallback to i32
let var04 = 5u8; // Initialization to unsigned 8 bit (u8) number 5
let var05: i8 = 5; // Type annotation + Initialization
let var06: u8 = 5u8; // Type annotation + Initialization
var01 = var05; // This line makes the compiler infer var01 should be a signed 8 bit number (i8).
var02 = 9u32; // Assigning to var02 after declaration
</syntaxhighlight>
Variables are immutable by default. So all the above declarations will all create an immutable variable that can't be reassigned once assigned for the first time. To make the variable mutable you need to add the "mut" keyword like in the following examples:
<syntaxhighlight lang="rust">
let mut var07;
let mut var08: f32;
let mut var09 = 1.5;
let mut var10 = 2.5f32;
let mut var11: f32 = 5.0;
let mut var12: f64 = 5.0f64;
var07 = var11; // This line makes the compiler infer var07 should be a f32.
var08 = 3.1416f32; // Assigning to var08 after declaration
var08 = 2.7183f32; // Reassigning var08 since it can be reassigned since it is mutable.
</syntaxhighlight>
The compiler emits a warning if a variable is not used. To suppress the warning, you need to put an underscore "_" prefix before the variable name:
<syntaxhighlight lang="rust">
let _notused = 10;
let mut _mutnotused = 42;
</syntaxhighlight>
 
=={{header|Scala}}==
Line 5,280 ⟶ 5,357:
Attributes <code>Public</code> or <code>Private</code> can mofidy these scopes.
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="textv (vlang)">name := 'Bob'
age := 20
large_number := i64(9999999999)</syntaxhighlight>
Line 5,293 ⟶ 5,370:
 
'''Mutable variables'''
<syntaxhighlight lang="textv (vlang)">mut age := 20
println(age) // 20
age = 21
Line 5,310 ⟶ 5,387:
'''Declaration errors'''
In development mode the compiler will warn you that you haven't used the variable (you'll get an "unused variable" warning). In production mode (enabled by passing the -prod flag to v – v -prod foo.v) it will not compile at all (like in Go).
<syntaxhighlight lang="textv (vlang)">fn main() {
a := 10
if true {
Line 5,357 ⟶ 5,434:
 
Here are a few examples.
<syntaxhighlight lang="ecmascriptwren">var a // declares the variable 'a' with the default value of 'null'
a = 1 // initializes 'a'
a = "a" // assigns a different value to 'a'
1,150

edits