Enforced immutability: Difference between revisions

no edit summary
No edit summary
Line 1,280:
Pi:= 3.15; \causes a compile error: statement starting with a constant
</lang>
 
=={{header|Vlang}}==
In Vlang:
 
1) Variables are immutable, by default.
 
2) Structs are immutable, by default.
 
3) Function arguments are immutable, by default.
 
4) Strings are immutable, and you can't mutate elements.
 
To change the values of variables, arguments, and struct fields the keyword "mut" is used.
<lang vlang>// To change the value of the variable, after making it mutable with "mut", use "=".
 
mut age := 20
println(age)
age = 21
println(age)
 
// For structs, we can define whether immutable or mutable by using the "mut" keyword.
// Outside of a function example:
 
struct Point {
mut:
x int
y int
}
// Inside of a function example:
 
mut p := Point{
x: 10
y: 20
}
 
// Function argument example:
 
fn (mut arg Point) register() {
println("Show the struct:\n $arg")
}
 
// V string individual elements are immutable, so we cannot assign to s[i], and will get an error.
 
mut s := 'hello'
s[0] = m // not allowed</lang>
 
=={{header|Wren}}==
291

edits