068_comptime3.zig 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // You can also put 'comptime' before a function parameter to
  3. // enforce that the argument passed to the function must be known
  4. // at compile time. We've actually been using a function like
  5. // this the entire time, std.debug.print():
  6. //
  7. // fn print(comptime fmt: []const u8, args: anytype) void
  8. //
  9. // Notice that the format string parameter 'fmt' is marked as
  10. // 'comptime'. One of the neat benefits of this is that the
  11. // format string can be checked for errors at compile time rather
  12. // than crashing at runtime.
  13. //
  14. // (The actual formatting is done by std.fmt.format() and it
  15. // contains a complete format string parser that runs entirely at
  16. // compile time!)
  17. //
  18. const print = @import("std").debug.print;
  19. // This struct is the model of a model boat. We can transform it
  20. // to any scale we would like: 1:2 is half-size, 1:32 is
  21. // thirty-two times smaller than the real thing, and so forth.
  22. const Schooner = struct {
  23. name: []const u8,
  24. scale: u32 = 1,
  25. hull_length: u32 = 143,
  26. bowsprit_length: u32 = 34,
  27. mainmast_height: u32 = 95,
  28. fn scaleMe(self: *Schooner, comptime scale: u32) void {
  29. comptime var my_scale = scale;
  30. // We did something neat here: we've anticipated the
  31. // possibility of accidentally attempting to create a
  32. // scale of 1:0. Rather than having this result in a
  33. // divide-by-zero error at runtime, we've turned this
  34. // into a compile error.
  35. //
  36. // This is probably the correct solution most of the
  37. // time. But our model boat model program is very casual
  38. // and we just want it to "do what I mean" and keep
  39. // working.
  40. //
  41. // Please change this so that it sets a 0 scale to 1
  42. // instead.
  43. if (my_scale == 0) @compileError("Scale 1:0 is not valid!");
  44. self.scale = my_scale;
  45. self.hull_length /= my_scale;
  46. self.bowsprit_length /= my_scale;
  47. self.mainmast_height /= my_scale;
  48. }
  49. fn printMe(self: Schooner) void {
  50. print("{s} (1:{}, {} x {})\n", .{
  51. self.name,
  52. self.scale,
  53. self.hull_length,
  54. self.mainmast_height,
  55. });
  56. }
  57. };
  58. pub fn main() void {
  59. var whale = Schooner{ .name = "Whale" };
  60. var shark = Schooner{ .name = "Shark" };
  61. var minnow = Schooner{ .name = "Minnow" };
  62. // Hey, we can't just pass this runtime variable as an
  63. // argument to the scaleMe() method. What would let us do
  64. // that?
  65. var scale: u32 = undefined;
  66. scale = 32; // 1:32 scale
  67. minnow.scaleMe(scale);
  68. minnow.printMe();
  69. scale -= 16; // 1:16 scale
  70. shark.scaleMe(scale);
  71. shark.printMe();
  72. scale -= 16; // 1:0 scale (oops, but DON'T FIX THIS!)
  73. whale.scaleMe(scale);
  74. whale.printMe();
  75. }
  76. //
  77. // Going deeper:
  78. //
  79. // What would happen if you DID attempt to build a model in the
  80. // scale of 1:0?
  81. //
  82. // A) You're already done!
  83. // B) You would suffer a mental divide-by-zero error.
  84. // C) You would construct a singularity and destroy the
  85. // planet.
  86. //
  87. // And how about a model in the scale of 0:1?
  88. //
  89. // A) You're already done!
  90. // B) You'd arrange nothing carefully into the form of the
  91. // original nothing but infinitely larger.
  92. // C) You would construct a singularity and destroy the
  93. // planet.
  94. //
  95. // Answers can be found on the back of the Ziglings packaging.