5.4. With Statement
DANGER
Experimental Feature
The with statement creates scoped blocks in which members of the given expression can be accessed without repeating its name.
bnf
with ( [ ( var | unowned var | type-name identifier ) = ] expression ) embedded_statementvala
struct Color {
int red;
int green;
int blue;
}
void main ()
{
Color c = {255, 0, 0};
print ("red: %d, green: %d, blue: %d\n", c.red, c.green, c.blue);
with (c) {
red = 1;
green = 2;
blue = 3;
}
// red: 1, green: 2, blue: 3
print ("red: %d, green: %d, blue: %d\n", c.red, c.green, c.blue);
}The with statement is a convenient way to access members of a struct or class without prefixing them with the object name.
WARNING
Use with statements carefully, as they can make programs harder to read and understand.
Variable Declarations in With Statement
It's also possible to do variable delcarations within with statement parentheses. Keeping the Color variable in the scope of the with statement block:
vala
public struct Color {
int red;
int green;
int blue;
}
void main ()
{
with (Color c = Color () { red = 1, green = 2, blue = 3 }) {
// red: 1, green: 2, blue: 3
print ("red: %d, green: %d, blue: %d\n", red, green, blue);
}
}