Type System
Twinkies is a statically-typed language, meaning every variable and function must have an explicit type that is checked at compile time.
Primitive Types
Integer Types
let x: int = 42; // Standard integer (platform-dependent size)
let a: int8 = 127; // 8-bit signed integer (-128 to 127)
let b: int16 = 32767; // 16-bit signed integer
let c: int32 = 2147483647; // 32-bit signed integer
let d: int64 = 9223372036854775807; // 64-bit signed integer
Floating-Point Types
let f: float = 3.14; // Single-precision floating point
let d: double = 2.718; // Double-precision floating point
Boolean Type
let flag: bool = true;
let isDone: bool = false;
String Type
let message: string = "Hello, World!";
let empty: string = "";
Null Type
The null literal can be assigned to any type:
let x: int = null;
let s: string = null;
let b: bool = null;
Array Types
Arrays are fixed-size and must specify both element type and size:
let numbers: int[5]; // Array of 5 integers
let flags: bool[10]; // Array of 10 booleans
let names: string[3]; // Array of 3 strings
let matrix: float[3][3]; // Multi-dimensional arrays (if supported)
Array indices start at 0:
let arr: int[5];
arr[0] = 10; // First element
arr[4] = 50; // Last element (0-4 are valid)
Type Checking
The compiler performs strict type checking:
let x: int = 10; // ✓ Correct
let y: bool = true; // ✓ Correct
let z: int = "hello"; // ✗ Error: cannot assign string to int
let w: bool = 42; // ✗ Error: cannot assign int to bool
Type Conversions
Automatic Numeric Conversions
Numeric types (int, float, double) can be automatically converted:
let i: int = 10;
let f: float = 3.14;
let result: double = i + f; // ✓ Automatic conversion
The compiler follows this promotion hierarchy:
- If either operand is
double, result isdouble - Else if either operand is
float, result isfloat - Otherwise, result is
int
Conversion Warnings
The compiler warns about potential precision loss:
let f: float = 3.14;
let i: int = f; // Warning: potential precision loss
Boolean Conversions
Booleans can be implicitly converted to/from integers (with warnings):
let x: int = true; // Warning: implicit conversion
let b: bool = 1; // Warning: implicit conversion
Null Compatibility
null is compatible with all types:
let x: int = null; // ✓ Valid
let s: string = null; // ✓ Valid
if (x == null) { ... } // ✓ Valid comparison
Type Inference
Twinkies requires explicit type annotations. There is no type inference:
let x: int = 10; // Type must be specified
// let y = 10; // ✗ Error: type annotation required
Function Types
Functions have types based on their parameters and return type:
// Function type: (int, int) -> int
func add(x: int, y: int) -> int {
return x + y;
}
Type Compatibility Rules
Assignment Compatibility
A value can be assigned to a variable if:
- Types match exactly, OR
- Both are numeric types, OR
- One is
null, OR - Implicit conversion is allowed (with warnings)
Operation Compatibility
Binary operations require compatible types:
let a: int = 10;
let b: int = 5;
let sum: int = a + b; // ✓ Both int
let f: float = 3.14;
let result: float = a + f; // ✓ Numeric types compatible
Comparison Compatibility
Comparisons work between:
- Same types
- Numeric types
nullwith any type
let x: int = 10;
let y: int = 5;
x > y; // ✓ Same type
let f: float = 3.14;
x > f; // ✓ Numeric types
x == null; // ✓ null compatible
Type Errors
The compiler reports type errors with helpful messages:
let x: bool = 42;
// Error: Type mismatch. Expected bool, got int.
// Suggestion: Use explicit conversion or change variable type.
Type Safety Features
Array Bounds Checking
Arrays are automatically bounds-checked:
let arr: int[5];
arr[4] = 10; // ✓ Valid (0-4 are valid indices)
arr[10] = 20; // ✗ Compile-time error: index out of bounds
For dynamic indices, runtime bounds checking is generated:
let i: int = input();
arr[i] = 10; // Runtime bounds check generated
Null Safety
The compiler tracks null usage but doesn't prevent null pointer dereferences (this is a limitation):
let s: string = null;
let len: int = strlen(s); // May cause runtime error
Type System Limitations
- No Generics - Types must be explicitly specified
- No Type Inference - All types must be explicit
- No Union Types - A variable can only have one type
- No Type Aliases - Cannot create type synonyms
Best Practices
- Always specify types explicitly - Makes code more readable
- Use appropriate types - Use
int32orint64when size matters - Handle null carefully - Check for null before using values
- Use type-safe operations - Let the compiler catch type errors
Next Steps
- Learn about Functions and their types
- Read Arrays and Strings for array type details
- Check Control Flow for type usage in conditionals
