Structuring Data
This chapter covers how Auto structures data using types, enums, and specs, and how these map to C's structs, unions, typedefs, and function pointer patterns. You will build real data structures and see how Auto's design prevents common errors.
31. Structures
C uses struct to group related fields:
struct Point {
int x;
int y;
};
struct Point p = {3, 4};Auto uses type to define structures. Fields are comma-separated and the constructor uses named initialization:
Auto's Point(3, 4) transpiles to C's (struct Point){.x = 3, .y = 4} using designated initializers. Methods like Point.modulus() become standalone functions Point_Modulus(struct Point *self) in C.
32. Unions
C unions allow different types to share the same memory:
union Value {
int i;
float f;
};Plain C unions are unsafe — there is no tag to tell which variant is active. Auto's enum creates tagged unions that carry both a tag and a value:
In C, enum Shape becomes a struct with a tag field and a union as field. The is expression transpiles to a switch on the tag.
33. Typedef vs Auto type
C uses typedef to create type aliases:
typedef int UserId;
typedef float Score;Auto's type with a single field creates the same effect without the typedef keyword:
Auto's wrapper types are more expressive than C's typedef because they create distinct types — you cannot accidentally pass a UserId where a plain int is expected.
34. Bitfields
C allows specifying the bit width of struct fields:
struct Flags {
unsigned int is_active : 1;
unsigned int priority : 3;
};Bitfields are a C-only feature for memory-constrained environments. Auto does not provide bitfields. Use bool and int fields instead; the compiler optimizes when possible.
35. Enumerations
C enum assigns integer constants:
enum Color { RED, GREEN, BLUE };
enum Color c = GREEN;Auto's enum (without payload) maps to C's enum with a NAME_VARIANT prefix to avoid name collisions:
enum Color { RED, GREEN, BLUE }becomes:
enum Color { COLOR_RED, COLOR_GREEN, COLOR_BLUE };36. Linked Lists
Linked lists are a fundamental data structure. Each node holds a value and a reference to the next node. Auto's ?T optional type naturally represents the "next or nothing" link.
In C, ?Node becomes struct Node* (a nullable pointer). The nil literal maps to NULL.
37. Stacks and Queues
A stack is a last-in, first-out (LIFO) data structure. Implement it with an array and a top index.
A queue would use two indices (front and rear) or a ring buffer. The principle is the same: wrap an array in a type and provide push/pop methods.
38. Hash Tables
A simple hash table uses an array of buckets and a hash function. In C, you would use function pointers for the hash and equality strategies. In Auto, the spec system provides this cleanly:
spec Hasher {
fn hash(key str) int
}Each bucket can be a linked list (for collision chaining). The full implementation is beyond this chapter's scope, but the pattern follows the same spec + type approach used in Chapter 2 for function pointers.
39. OOP in C and Auto's spec/type
C does not have classes, but you can simulate object-oriented programming with structs and function pointers. Auto's spec and type system makes this idiomatic and type-safe.
The spec Drawable generates a vtable struct in C with function pointers. Each implementing type (CircleObj, SquareObj) gets a vtable instance and concrete method functions.
40. Practice: Library System
Build a small library management system using types, methods, and state management.
This exercise combines type definitions, methods, state mutation, and conditional logic — all the concepts from this chapter.
Quick Reference
| Concept | Auto | C |
|---|---|---|
| Structure | type Name { fields } | struct Name { fields }; |
| Constructor | Name(v1, v2) | (struct Name){.f1=v1, .f2=v2} |
| Method | fn Name.method() | void Name_method(struct Name *self) |
| Tagged union | enum Tag { Variant fields } | struct Tag { tag; union as; } |
| Type alias | type Name BaseType | typedef BaseType Name; |
| Enumeration | enum Color { RED } | enum Color { COLOR_RED }; |
| Optional | ?T | T* (nullable) |
| Linked list node | type Node { val, ?Node } | struct Node { val; Node* next; } |
| Interface | spec Name { fn ... } | vtable with function pointers |
| Implementation | type Foo as Spec { } | vtable instance + methods |
| Null | nil | NULL |
| Field access | self.field | self->field |