mirror of
https://codeberg.org/ziglings/exercises.git
synced 2024-12-27 01:46:29 +00:00
Compare commits
6 commits
71b4f5ea81
...
150b3de299
Author | SHA1 | Date | |
---|---|---|---|
|
150b3de299 | ||
|
fb018d212c | ||
|
d0d31ae73a | ||
|
530dcde3d4 | ||
|
286439cddc | ||
|
f629d78268 |
2 changed files with 16 additions and 14 deletions
|
@ -110,15 +110,15 @@ pub fn main() void {
|
||||||
// name will not be printed if the field is of type 'void'
|
// 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!):
|
// (which is a zero-bit type that takes up no space at all!):
|
||||||
if (fields[0].??? != void) {
|
if (fields[0].??? != void) {
|
||||||
print(" {s}", .{@typeInfo(Narcissus).@"struct".fields[0].name});
|
print(" {s}", .{fields[0].name});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fields[1].??? != void) {
|
if (fields[1].??? != void) {
|
||||||
print(" {s}", .{@typeInfo(Narcissus).@"struct".fields[1].name});
|
print(" {s}", .{fields[1].name});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fields[2].??? != void) {
|
if (fields[2].??? != void) {
|
||||||
print(" {s}", .{@typeInfo(Narcissus).@"struct".fields[2].name});
|
print(" {s}", .{fields[2].name});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Yuck, look at all that repeated code above! I don't know
|
// Yuck, look at all that repeated code above! I don't know
|
||||||
|
@ -136,14 +136,16 @@ pub fn main() void {
|
||||||
// But a change after Zig 0.10.0 added the source file name to the
|
// But a change after Zig 0.10.0 added the source file name to the
|
||||||
// type. "Narcissus" became "065_builtins2.Narcissus".
|
// type. "Narcissus" became "065_builtins2.Narcissus".
|
||||||
//
|
//
|
||||||
// To fix this, I've added this function to strip the filename from
|
// To fix this, we've added this function to strip the filename from
|
||||||
// the front of the type name in the dumbest way possible. (It returns
|
// the front of the type name. (It returns a slice of the type name
|
||||||
// a slice of the type name starting at character 14 (assuming
|
// starting at the index + 1 of character ".")
|
||||||
// single-byte characters).
|
|
||||||
//
|
//
|
||||||
// We'll be seeing @typeName again in Exercise 070. For now, you can
|
// We'll be seeing @typeName again in Exercise 070. For now, you can
|
||||||
// see that it takes a Type and returns a u8 "string".
|
// see that it takes a Type and returns a u8 "string".
|
||||||
fn maximumNarcissism(myType: anytype) []const u8 {
|
fn maximumNarcissism(myType: anytype) []const u8 {
|
||||||
// Turn '065_builtins2.Narcissus' into 'Narcissus'
|
const indexOf = @import("std").mem.indexOf;
|
||||||
return @typeName(myType)[14..];
|
|
||||||
|
// Turn "065_builtins2.Narcissus" into "Narcissus"
|
||||||
|
const name = @typeName(myType);
|
||||||
|
return name[indexOf(u8, name, ".").? + 1 ..];
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
--- exercises/065_builtins2.zig 2024-09-02 19:15:56.569952315 +0200
|
--- exercises/065_builtins2.zig 2024-11-02 16:58:30.607829441 +0100
|
||||||
+++ answers/065_builtins2.zig 2024-09-02 19:13:44.280600350 +0200
|
+++ answers/065_builtins2.zig 2024-11-02 16:58:33.821220588 +0100
|
||||||
@@ -58,7 +58,7 @@
|
@@ -58,7 +58,7 @@
|
||||||
// Oops! We cannot leave the 'me' and 'myself' fields
|
// Oops! We cannot leave the 'me' and 'myself' fields
|
||||||
// undefined. Please set them here:
|
// undefined. Please set them here:
|
||||||
|
@ -24,16 +24,16 @@
|
||||||
// (which is a zero-bit type that takes up no space at all!):
|
// (which is a zero-bit type that takes up no space at all!):
|
||||||
- if (fields[0].??? != void) {
|
- if (fields[0].??? != void) {
|
||||||
+ if (fields[0].type != void) {
|
+ if (fields[0].type != void) {
|
||||||
print(" {s}", .{@typeInfo(Narcissus).@"struct".fields[0].name});
|
print(" {s}", .{fields[0].name});
|
||||||
}
|
}
|
||||||
|
|
||||||
- if (fields[1].??? != void) {
|
- if (fields[1].??? != void) {
|
||||||
+ if (fields[1].type != void) {
|
+ if (fields[1].type != void) {
|
||||||
print(" {s}", .{@typeInfo(Narcissus).@"struct".fields[1].name});
|
print(" {s}", .{fields[1].name});
|
||||||
}
|
}
|
||||||
|
|
||||||
- if (fields[2].??? != void) {
|
- if (fields[2].??? != void) {
|
||||||
+ if (fields[2].type != void) {
|
+ if (fields[2].type != void) {
|
||||||
print(" {s}", .{@typeInfo(Narcissus).@"struct".fields[2].name});
|
print(" {s}", .{fields[2].name});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue