Skip to content

Spontaneous Variable Declarations

IsaacShelton edited this page Mar 21, 2022 · 1 revision

Spontaneous Variable Declarations

Spontaneous variable declarations allow for variables to be declared within expressions.

def operation_succeeded successful
undef raw_data RawData 

Usage example

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
}

Result Type

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.

def vs undef

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.

Clone this wiki locally