Exercise 11-1. Write a generic min function for values that support <.
fn min(a: anytype, b: @TypeOf(a)) @TypeOf(a)Return the smaller value.
Test it with:
i32u64f64
Exercise 11-2. Write a generic swap function.
The function should exchange two values using pointers.
fn swap(a: anytype, b: anytype) voidTest it with integers and floating-point values.
Exercise 11-3. Write a generic max3 function.
fn max3(a: anytype, b: @TypeOf(a), c: @TypeOf(a)) @TypeOf(a)Return the largest of three values.
Exercise 11-4. Modify this function so it accepts only integer types:
fn add(a: anytype, b: @TypeOf(a)) @TypeOf(a)Use @typeInfo.
Print a compile-time error for unsupported types.
Exercise 11-5. Write a generic Point struct.
Example usage:
const IntPoint = Point(i32);
var p = IntPoint{
.x = 10,
.y = 20,
};Add a method that prints the coordinates.
Exercise 11-6. Add a clear method to the generic Stack type.
The method should reset the length to zero without modifying stored elements.
Exercise 11-7. Write a generic fixed-size queue.
Requirements:
- compile-time element type
- compile-time capacity
pushpopisEmpty
Use a circular buffer.
Exercise 11-8. Write a generic counter wrapper.
The struct should:
- store a value
- track update count
- provide a method to assign a new value
Example:
var c = Counter(i32){
.value = 0,
.updates = 0,
};Exercise 11-9. Write a generic function that accepts only floating-point types.
Use:
@typeInfoReject all other types with:
@compileErrorExercise 11-10. Write a function that accepts only pointers.
The function should print whether the pointer is null.
Exercise 11-11. Write a generic sum function for arrays.
Example:
const values = [_]i32{ 1, 2, 3, 4 };Return the sum of all elements.
Exercise 11-12. Write a compile-time check requiring a declaration named init.
Use:
@hasDeclTest it with:
- one valid type
- one invalid type
Exercise 11-13. Write a generic function that calls next() on its argument.
The function should fail at compile time if the declaration does not exist.
Exercise 11-14. Extend Exercise 11-13.
Require both:
readwrite
Produce separate compile-time errors for each missing declaration.
Exercise 11-15. Implement a runtime-dispatched logger.
The logger should contain:
- an erased object pointer
- a function pointer for writing messages
Implement two backends:
- console logger
- memory logger
Exercise 11-16. Write two unrelated structs satisfying the same interface convention.
Both should provide:
process()Write one generic function that calls the method.
Exercise 11-17. Write a generic function that prints type categories.
Possible outputs:
- integer
- float
- pointer
- struct
- other
Use @typeInfo.
Exercise 11-18. Write a generic function that distinguishes between signed and unsigned integers.
Hint:
.int.signednessExercise 11-19. Implement a small generic formatter.
Create a struct:
const Person = struct {
name: []const u8,
age: u8,
};Write a formatting function that prints the fields.
Exercise 11-20. Write a compile-time debug flag.
Example:
fn log(comptime enabled: bool, message: []const u8) voidWhen enabled is false, no logging code should remain in the generated program.