Structs

Structs (short for "structures") in Type-C serve as a composite data type that enables developers to group multiple variables of disparate types under a single identifier. This promotes ease of manipulation and management for related data fields. In Type-C, the struct keyword is optionally used to define a new struct type. Within braces, fields are specified with their associated data types. Hence, declaring a type without the struct keyword is also valid.

Structs in Type-C can also contain functions as attributes. However, these functions don't operate with the context of the struct's current state, unlike methods in objects.

Generic Structs

Type-C also supports generic structs, allowing for greater flexibility.

Type-C is flexible in how structs are assigned values:

  • Case 1: If the variable type is already set, field names can be omitted in assignments as long as the field values are in the correct order.
  • Case 2: If the variable type is unspecified, field names must be explicitly stated.

Structs Compatibility

Structs are compatible with other structs if they have the same fields and compatible types. This allows for easy conversion between structs.

The above structs are compatible, and can be converted to each other.

Additionally, two structs struct1 and struct2 are compatible if struct2 has all the fields of struct1 and the types of the fields are compatible. This allows for easy conversion between structs.


Kudos! Keep reading!