summaryrefslogtreecommitdiff
path: root/exercises
diff options
context:
space:
mode:
Diffstat (limited to 'exercises')
-rw-r--r--exercises/065_builtins2.zig33
-rw-r--r--exercises/071_comptime6.zig9
-rw-r--r--exercises/082_anonymous_structs3.zig31
3 files changed, 27 insertions, 46 deletions
diff --git a/exercises/065_builtins2.zig b/exercises/065_builtins2.zig
index eb8f3aa..78cd23b 100644
--- a/exercises/065_builtins2.zig
+++ b/exercises/065_builtins2.zig
@@ -93,36 +93,25 @@ pub fn main() void {
print("He has room in his heart for:", .{});
- // A StructFields array
- const fields = @typeInfo(Narcissus).@"struct".fields;
+ // `field_names` is a slice of strings and it holds the names of the struct's fields
+ // `field_types` is a slice of strings and it holds the types of the struct's fields,
+ // it is guaranteed to be the same length as `field_names`
+ const field_names = @typeInfo(Narcissus).@"struct".field_names;
+ const field_types = @typeInfo(Narcissus).@"struct".field_types;
- // 'fields' is a slice of StructFields. Here's the declaration:
- //
- // pub const StructField = struct {
- // name: [:0]const u8, // Don't worry about `:0` yet!
- // type: type,
- // default_value_ptr: ?*const anyopaque,
- // is_comptime: bool,
- // alignment: comptime_int,
- //
- // defaultValue() ?sf.type // Function that loads the
- // // field's default value from
- // // `default_value_ptr`
- // };
- //
// Please complete these 'if' statements so that the field
// name will not be printed if the field is of type 'void'
// (which is a zero-bit type that takes up no space at all!):
- if (fields[0].??? != void) {
- print(" {s}", .{fields[0].name});
+ if (field_???[???] != void) {
+ print(" {s}", .{field_???[???]});
}
- if (fields[1].??? != void) {
- print(" {s}", .{fields[1].name});
+ if (field_???[???] != void) {
+ print(" {s}", .{field_???[???]});
}
- if (fields[2].??? != void) {
- print(" {s}", .{fields[2].name});
+ if (field_???[???] != void) {
+ print(" {s}", .{field_???[???]});
}
// Yuck, look at all that repeated code above! I don't know
diff --git a/exercises/071_comptime6.zig b/exercises/071_comptime6.zig
index ce76607..3df9299 100644
--- a/exercises/071_comptime6.zig
+++ b/exercises/071_comptime6.zig
@@ -36,13 +36,14 @@ pub fn main() void {
// statement was repeated three times almost verbatim. Yuck!
//
// Please use an 'inline for' to implement the block below
- // for each field in the slice 'fields'!
+ // for each field in the corresponding slices (they're of the same length)!
- const fields = @typeInfo(Narcissus).@"struct".fields;
+ const field_names = @typeInfo(Narcissus).@"struct".field_names;
+ const field_types = @typeInfo(Narcissus).@"struct".field_types;
??? {
- if (field.type != void) {
- print(" {s}", .{field.name});
+ if (field_type != void) {
+ print(" {s}", .{field_name});
}
}
diff --git a/exercises/082_anonymous_structs3.zig b/exercises/082_anonymous_structs3.zig
index c13774f..e99c826 100644
--- a/exercises/082_anonymous_structs3.zig
+++ b/exercises/082_anonymous_structs3.zig
@@ -74,36 +74,27 @@ fn printTuple(tuple: anytype) void {
// @typeInfo() - takes a type, returns a TypeInfo union
// with fields specific to that type.
//
- // The list of a struct type's fields can be found in
- // TypeInfo's @"struct".fields.
+ // The list of a struct type's field types can be found in
+ // TypeInfo's @"struct".field_types.
//
// Example:
//
- // @typeInfo(Circle).@"struct".fields
+ // @typeInfo(Circle).@"struct".field_types
//
- // This will be an array of StructFields.
- const fields = ???;
+ // This will be an array of field types.
+ 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
// time.
//
// Hint: remember 'inline' loops?
//
- for (fields) |field| {
+ for (???, ???) |???, ???| {
// 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:
//
// @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.
//
print("\"{s}\"({any}):{any} ", .{
- field.???,
- field.???,
+ field_name,
+ field_type,
???,
});
}