mirror of
https://codeberg.org/ziglings/exercises.git
synced 2026-06-08 07:50:00 +00:00
Fix 082_anonymous_structs3.zig because of new build system
This commit is contained in:
@@ -74,36 +74,27 @@ fn printTuple(tuple: anytype) void {
|
|||||||
// @typeInfo() - takes a type, returns a TypeInfo union
|
// @typeInfo() - takes a type, returns a TypeInfo union
|
||||||
// with fields specific to that type.
|
// with fields specific to that type.
|
||||||
//
|
//
|
||||||
// The list of a struct type's fields can be found in
|
// The list of a struct type's field types can be found in
|
||||||
// TypeInfo's @"struct".fields.
|
// TypeInfo's @"struct".field_types.
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// @typeInfo(Circle).@"struct".fields
|
// @typeInfo(Circle).@"struct".field_types
|
||||||
//
|
//
|
||||||
// This will be an array of StructFields.
|
// This will be an array of field types.
|
||||||
const fields = ???;
|
const field_types = ???;
|
||||||
|
|
||||||
|
// This will be an array of field names.
|
||||||
|
const field_names = ???;
|
||||||
|
|
||||||
// 2. Loop through each field. This must be done at compile
|
// 2. Loop through each field. This must be done at compile
|
||||||
// time.
|
// time.
|
||||||
//
|
//
|
||||||
// Hint: remember 'inline' loops?
|
// Hint: remember 'inline' loops?
|
||||||
//
|
//
|
||||||
for (fields) |field| {
|
for (???, ???) |???, ???| {
|
||||||
// 3. Print the field's name, type, and value.
|
// 3. Print the field's name, type, and value.
|
||||||
//
|
//
|
||||||
// Each 'field' in this loop is one of these:
|
|
||||||
//
|
|
||||||
// pub const StructField = struct {
|
|
||||||
// name: [:0]const u8,
|
|
||||||
// type: type,
|
|
||||||
// default_value_ptr: ?*const anyopaque,
|
|
||||||
// is_comptime: bool,
|
|
||||||
// alignment: comptime_int,
|
|
||||||
// };
|
|
||||||
//
|
|
||||||
// Note we will learn about 'anyopaque' type later
|
|
||||||
//
|
|
||||||
// You'll need this builtin:
|
// You'll need this builtin:
|
||||||
//
|
//
|
||||||
// @field(lhs: anytype, comptime field_name: []const u8)
|
// @field(lhs: anytype, comptime field_name: []const u8)
|
||||||
@@ -123,8 +114,8 @@ fn printTuple(tuple: anytype) void {
|
|||||||
// for declarations. If it's a value, it looks for data.
|
// for declarations. If it's a value, it looks for data.
|
||||||
//
|
//
|
||||||
print("\"{s}\"({any}):{any} ", .{
|
print("\"{s}\"({any}):{any} ", .{
|
||||||
field.???,
|
field_name,
|
||||||
field.???,
|
field_type,
|
||||||
???,
|
???,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,32 +1,32 @@
|
|||||||
--- exercises/082_anonymous_structs3.zig 2026-02-27 13:05:46
|
--- exercises/082_anonymous_structs3.zig 2026-06-01 15:59:11.872467805 +0200
|
||||||
+++ answers/082_anonymous_structs3.zig 2026-02-27 13:07:22
|
+++ answers/082_anonymous_structs3.zig 2026-06-01 15:58:38.004730144 +0200
|
||||||
@@ -82,14 +82,14 @@
|
@@ -82,17 +82,17 @@
|
||||||
// @typeInfo(Circle).@"struct".fields
|
// @typeInfo(Circle).@"struct".field_types
|
||||||
//
|
//
|
||||||
// This will be an array of StructFields.
|
// This will be an array of field types.
|
||||||
- const fields = ???;
|
- const field_types = ???;
|
||||||
+ const fields = @typeInfo(@TypeOf(tuple)).@"struct".fields;
|
+ const field_types = @typeInfo(@TypeOf(tuple)).@"struct".field_types;
|
||||||
|
|
||||||
|
// This will be an array of field names.
|
||||||
|
- const field_names = ???;
|
||||||
|
+ const field_names = @typeInfo(@TypeOf(tuple)).@"struct".field_names;
|
||||||
|
|
||||||
// 2. Loop through each field. This must be done at compile
|
// 2. Loop through each field. This must be done at compile
|
||||||
// time.
|
// time.
|
||||||
//
|
//
|
||||||
// Hint: remember 'inline' loops?
|
// Hint: remember 'inline' loops?
|
||||||
//
|
//
|
||||||
- for (fields) |field| {
|
- for (???, ???) |???, ???| {
|
||||||
+ inline for (fields) |field| {
|
+ inline for (field_types, field_names) |field_type, field_name| {
|
||||||
// 3. Print the field's name, type, and value.
|
// 3. Print the field's name, type, and value.
|
||||||
//
|
//
|
||||||
// Each 'field' in this loop is one of these:
|
// You'll need this builtin:
|
||||||
@@ -123,9 +123,9 @@
|
@@ -116,7 +116,7 @@
|
||||||
// for declarations. If it's a value, it looks for data.
|
|
||||||
//
|
|
||||||
print("\"{s}\"({any}):{any} ", .{
|
print("\"{s}\"({any}):{any} ", .{
|
||||||
- field.???,
|
field_name,
|
||||||
- field.???,
|
field_type,
|
||||||
- ???,
|
- ???,
|
||||||
+ field.name,
|
+ @field(tuple, field_name),
|
||||||
+ field.type,
|
|
||||||
+ @field(tuple, field.name),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user