-
-
Notifications
You must be signed in to change notification settings - Fork 9
Spontaneous Variable Declarations
IsaacShelton edited this page Mar 21, 2022
·
1 revision
Spontaneous variable declarations allow for variables to be declared within expressions.
def operation_succeeded successful
undef raw_data RawData
import basics
func main {
getFavoriteColor(undef red float, undef green float, undef blue float)
print("Red-ness of favorite color: " + red)
print("Green-ness of favorite color: " + green)
print("Blue-ness of favorite color: " + blue)
}
func getFavoriteColor(r, g, b *float) {
*r = 1.0f
*g = 0.2f
*b = 0.6f
}
The resulting value will be a pointer to the newly declared variable.
For example, def x int
will result in a *int
with the same value as the value of &x
.
There are two types of spontaneous variable declarations:
def my_variable Type
undef my_variable Type
The difference between them, is that variables declared with def
will be zero-initialized, where as variables declared with undef
will be left uninitialized.
It's recommended to use def
instead of undef
, unless you're certain that undef
is safe.