mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-11-09 11:40:46 -05:00
Ex 080: Strip filename from @typeName output to address #130
This commit is contained in:
parent
4eaef5fae4
commit
1691b22c1b
2 changed files with 30 additions and 11 deletions
|
@ -7,7 +7,7 @@
|
||||||
//
|
//
|
||||||
// const Foo = struct {};
|
// const Foo = struct {};
|
||||||
//
|
//
|
||||||
// * The value of @typeName(Foo) is "Foo".
|
// * The value of @typeName(Foo) is "<filename>.Foo".
|
||||||
//
|
//
|
||||||
// A struct is also given a name when you return it from a
|
// A struct is also given a name when you return it from a
|
||||||
// function:
|
// function:
|
||||||
|
@ -61,16 +61,25 @@ pub fn main() void {
|
||||||
};
|
};
|
||||||
|
|
||||||
print("[{s}: {},{},{}] ", .{
|
print("[{s}: {},{},{}] ", .{
|
||||||
@typeName(@TypeOf(circle1)),
|
stripFname(@typeName(@TypeOf(circle1))),
|
||||||
circle1.center_x,
|
circle1.center_x,
|
||||||
circle1.center_y,
|
circle1.center_y,
|
||||||
circle1.radius,
|
circle1.radius,
|
||||||
});
|
});
|
||||||
|
|
||||||
print("[{s}: {d:.1},{d:.1},{d:.1}]\n", .{
|
print("[{s}: {d:.1},{d:.1},{d:.1}]\n", .{
|
||||||
@typeName(@TypeOf(circle2)),
|
stripFname(@typeName(@TypeOf(circle2))),
|
||||||
circle2.center_x,
|
circle2.center_x,
|
||||||
circle2.center_y,
|
circle2.center_y,
|
||||||
circle2.radius,
|
circle2.radius,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Perhaps you remember the "narcissistic fix" for the type name
|
||||||
|
// in Ex. 065? We're going to do the same thing here: use a hard-
|
||||||
|
// coded slice to return the type name. That's just so our output
|
||||||
|
// look prettier. Indulge your vanity. Programmers are beautiful.
|
||||||
|
fn stripFname(mytype: []const u8) []const u8 {
|
||||||
|
return mytype[22..];
|
||||||
|
}
|
||||||
|
// The above would be an instant red flag in a "real" program.
|
||||||
|
|
|
@ -1,8 +1,18 @@
|
||||||
51c51
|
--- exercises/080_anonymous_structs.zig
|
||||||
< var circle1 = ??? {
|
+++ answers/080_anonymous_structs.zig
|
||||||
---
|
@@ -48,13 +48,13 @@
|
||||||
> var circle1 = Circle(i32) {
|
// * circle1 should hold i32 integers
|
||||||
57c57
|
// * circle2 should hold f32 floats
|
||||||
< var circle2 = ??? {
|
//
|
||||||
---
|
- var circle1 = ??? {
|
||||||
> var circle2 = Circle(f32) {
|
+ var circle1 = Circle(i32){
|
||||||
|
.center_x = 25,
|
||||||
|
.center_y = 70,
|
||||||
|
.radius = 15,
|
||||||
|
};
|
||||||
|
|
||||||
|
- var circle2 = ??? {
|
||||||
|
+ var circle2 = Circle(f32){
|
||||||
|
.center_x = 25.234,
|
||||||
|
.center_y = 70.999,
|
||||||
|
.radius = 15.714,
|
||||||
|
|
Loading…
Reference in a new issue