struct, variant and array are aggregate types in m. Their data are stored on the stack by default. M uses call-by-value convention to pass arguments to the called function. Struct type in m is a value type. When the passed argument is struct type, the struct data is duplicated and its reference/address is passed to the function. This is to prevent the struct value is being changed by the callee. Array type is a reference type in m. When you assign an array object to a variable, only its address is assigned to the variable. The operation is super efficlient and this is in contrast to struct type assignment, where its contents are copied to the assigned variable.
complex cf64 is a builtin struct type defined as "struct cf64 = re:f64, im:f64", the example below shows how to initialize a struct type variable and how to access field of the struct type variable.
use keyword "struct" to define a struct type
use keyword "variant" to define a variant (tagged union) type
Struct could be nested. Here we embed struct cf64 inside struct AB.
return a nesting struct from a function
We define struct AB as two fields, each of which is also a cf64 struct. The following code is initializing a struct AB object and return one of field a to the caller.
One more time, but this time we eliminate a variable in the function.
pass a struct and return a new struct to the caller then print the field of returned struct
pass a struct and return a new struct the caller without any temp variable
struct member assign struct
variable scope
use field of struct in expression.
Here we define a function returning a struct type cf64 and assign it to variable x.
We can directly access return of function call without assigning a variable.
We can pass struct argument to a function.
We can pass struct argument directly without a variable to a function.
Pass the struct data, and return with expression using one field
Pass the struct data, and return with a struct with new value
struct member assign
initialize a 1 length of int array
initialize a two elements of int array
sum a two elements of int array
update the first element of an array
update the second element of an array
declare a u8 array
declare a u8 array
declare a u8 two dimensions array
update two dimensions array
update two dimensions array using variable
update two dimensions int array using variable
pass array variable to a function.
define a tuple, and access like array
construct a tuple, and unpack its fields
construct a tuple variable, and unpack fields of tuple variable
return a tuple from a function
pass a tuple to a function, and return a new tuple from the function