2021-04-11 11:19:58 -04:00
|
|
|
//
|
|
|
|
// Zig has builtins for mathematical operations such as...
|
|
|
|
//
|
2023-06-22 08:11:24 -04:00
|
|
|
// @sqrt @sin @cos
|
|
|
|
// @exp @log @floor
|
2021-04-11 11:19:58 -04:00
|
|
|
//
|
|
|
|
// ...and lots of type casting operations such as...
|
|
|
|
//
|
2023-06-22 08:11:24 -04:00
|
|
|
// @as @errorFromInt @floatFromInt
|
|
|
|
// @ptrFromInt @intFromPtr @intFromEnum
|
2021-04-11 11:19:58 -04:00
|
|
|
//
|
|
|
|
// Spending part of a rainy day skimming through the complete
|
|
|
|
// list of builtins in the official Zig documentation wouldn't be
|
|
|
|
// a bad use of your time. There are some seriously cool features
|
|
|
|
// in there. Check out @call, @compileLog, @embedFile, and @src!
|
|
|
|
//
|
|
|
|
// ...
|
|
|
|
//
|
|
|
|
// For now, we're going to complete our examination of builtins
|
|
|
|
// by exploring just THREE of Zig's MANY introspection abilities:
|
|
|
|
//
|
|
|
|
// 1. @This() type
|
2021-11-05 11:44:29 -04:00
|
|
|
//
|
2021-04-11 11:19:58 -04:00
|
|
|
// Returns the innermost struct, enum, or union that a function
|
|
|
|
// call is inside.
|
|
|
|
//
|
2023-10-08 12:51:22 -04:00
|
|
|
// 2. @typeInfo(comptime T: type) @import("std").builtin.Type
|
2021-11-05 11:44:29 -04:00
|
|
|
//
|
2023-10-08 12:51:22 -04:00
|
|
|
// Returns information about any type in a data structure which
|
2021-04-11 11:19:58 -04:00
|
|
|
// will contain different information depending on which type
|
|
|
|
// you're examining.
|
2021-11-05 11:44:29 -04:00
|
|
|
//
|
2021-04-11 11:19:58 -04:00
|
|
|
// 3. @TypeOf(...) type
|
2021-11-05 11:44:29 -04:00
|
|
|
//
|
2021-04-11 11:19:58 -04:00
|
|
|
// Returns the type common to all input parameters (each of which
|
|
|
|
// may be any expression). The type is resolved using the same
|
|
|
|
// "peer type resolution" process the compiler itself uses when
|
|
|
|
// inferring types.
|
|
|
|
//
|
|
|
|
// (Notice how the two functions which return types start with
|
|
|
|
// uppercase letters? This is a standard naming practice in Zig.)
|
|
|
|
//
|
2022-08-29 18:30:38 -04:00
|
|
|
const print = @import("std").debug.print;
|
2021-04-11 11:19:58 -04:00
|
|
|
|
|
|
|
const Narcissus = struct {
|
|
|
|
me: *Narcissus = undefined,
|
|
|
|
myself: *Narcissus = undefined,
|
2022-08-29 18:30:38 -04:00
|
|
|
echo: void = undefined, // Alas, poor Echo!
|
2021-11-05 11:44:29 -04:00
|
|
|
|
2021-04-11 11:19:58 -04:00
|
|
|
fn fetchTheMostBeautifulType() type {
|
|
|
|
return @This();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn main() void {
|
2021-11-05 12:37:12 -04:00
|
|
|
var narcissus: Narcissus = Narcissus{};
|
2021-04-11 11:19:58 -04:00
|
|
|
|
|
|
|
// Oops! We cannot leave the 'me' and 'myself' fields
|
|
|
|
// undefined. Please set them here:
|
2022-08-29 18:30:38 -04:00
|
|
|
narcissus.me = &narcissus;
|
2024-05-19 10:47:16 -04:00
|
|
|
narcissus.myself = &narcissus;
|
2021-04-11 11:19:58 -04:00
|
|
|
|
|
|
|
// This determines a "peer type" from three separate
|
|
|
|
// references (they just happen to all be the same object).
|
2022-08-29 18:30:38 -04:00
|
|
|
const Type1 = @TypeOf(narcissus, narcissus.me.*, narcissus.myself.*);
|
2021-04-11 11:19:58 -04:00
|
|
|
|
|
|
|
// Oh dear, we seem to have done something wrong when calling
|
2022-08-29 18:30:38 -04:00
|
|
|
// this function. We called it as a method, which would work
|
|
|
|
// if it had a self parameter. But it doesn't. (See above.)
|
|
|
|
//
|
|
|
|
// The fix for this is very subtle, but it makes a big
|
|
|
|
// difference!
|
2024-05-19 10:47:16 -04:00
|
|
|
const Type2 = Narcissus.fetchTheMostBeautifulType();
|
2021-04-11 11:19:58 -04:00
|
|
|
|
2022-08-29 18:30:38 -04:00
|
|
|
// Now we print a pithy statement about Narcissus.
|
|
|
|
print("A {s} loves all {s}es. ", .{
|
|
|
|
maximumNarcissism(Type1),
|
|
|
|
maximumNarcissism(Type2),
|
|
|
|
});
|
2021-04-11 11:19:58 -04:00
|
|
|
|
|
|
|
// His final words as he was looking in
|
|
|
|
// those waters he habitually watched
|
|
|
|
// were these:
|
|
|
|
// "Alas, my beloved boy, in vain!"
|
|
|
|
// The place gave every word back in reply.
|
|
|
|
// He cried:
|
|
|
|
// "Farewell."
|
|
|
|
// And Echo called:
|
|
|
|
// "Farewell!"
|
|
|
|
//
|
|
|
|
// --Ovid, The Metamorphoses
|
|
|
|
// translated by Ian Johnston
|
|
|
|
|
|
|
|
print("He has room in his heart for:", .{});
|
|
|
|
|
|
|
|
// A StructFields array
|
|
|
|
const fields = @typeInfo(Narcissus).Struct.fields;
|
|
|
|
|
2021-04-22 17:17:25 -04:00
|
|
|
// 'fields' is a slice of StructFields. Here's the declaration:
|
2021-04-11 11:19:58 -04:00
|
|
|
//
|
|
|
|
// pub const StructField = struct {
|
|
|
|
// name: []const u8,
|
2023-01-14 09:36:36 -05:00
|
|
|
// type: type,
|
2021-04-11 11:19:58 -04:00
|
|
|
// default_value: anytype,
|
|
|
|
// is_comptime: bool,
|
|
|
|
// alignment: comptime_int,
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// 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!):
|
2024-05-19 10:47:16 -04:00
|
|
|
if (fields[0].type != void) {
|
2021-04-11 11:19:58 -04:00
|
|
|
print(" {s}", .{@typeInfo(Narcissus).Struct.fields[0].name});
|
|
|
|
}
|
|
|
|
|
2024-05-19 10:47:16 -04:00
|
|
|
if (fields[1].type != void) {
|
2021-04-11 11:19:58 -04:00
|
|
|
print(" {s}", .{@typeInfo(Narcissus).Struct.fields[1].name});
|
|
|
|
}
|
|
|
|
|
2024-05-19 10:47:16 -04:00
|
|
|
if (fields[2].type != void) {
|
2021-04-11 11:19:58 -04:00
|
|
|
print(" {s}", .{@typeInfo(Narcissus).Struct.fields[2].name});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Yuck, look at all that repeated code above! I don't know
|
|
|
|
// about you, but it makes me itchy.
|
|
|
|
//
|
|
|
|
// Alas, we can't use a regular 'for' loop here because
|
|
|
|
// 'fields' can only be evaluated at compile time. It seems
|
|
|
|
// like we're overdue to learn about this "comptime" stuff,
|
2022-08-29 18:30:38 -04:00
|
|
|
// doesn't it? Don't worry, we'll get there.
|
2021-04-11 11:19:58 -04:00
|
|
|
|
|
|
|
print(".\n", .{});
|
|
|
|
}
|
2022-08-29 18:30:38 -04:00
|
|
|
|
|
|
|
// NOTE: This exercise did not originally include the function below.
|
|
|
|
// But a change after Zig 0.10.0 added the source file name to the
|
|
|
|
// type. "Narcissus" became "065_builtins2.Narcissus".
|
|
|
|
//
|
|
|
|
// To fix this, I've added this function to strip the filename from
|
|
|
|
// the front of the type name in the dumbest way possible. (It returns
|
|
|
|
// a slice of the type name starting at character 14 (assuming
|
|
|
|
// single-byte characters).
|
|
|
|
//
|
|
|
|
// We'll be seeing @typeName again in Exercise 070. For now, you can
|
|
|
|
// see that it takes a Type and returns a u8 "string".
|
|
|
|
fn maximumNarcissism(myType: anytype) []const u8 {
|
|
|
|
// Turn '065_builtins2.Narcissus' into 'Narcissus'
|
|
|
|
return @typeName(myType)[14..];
|
|
|
|
}
|