mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-12-22 06:03:09 -05:00
066-071 completed
This commit is contained in:
parent
dcde00e30c
commit
9c26298549
6 changed files with 11 additions and 10 deletions
|
@ -62,8 +62,8 @@ pub fn main() void {
|
||||||
// types with specific sizes. The comptime numbers will be
|
// types with specific sizes. The comptime numbers will be
|
||||||
// coerced (if they'll fit!) into your chosen runtime types.
|
// coerced (if they'll fit!) into your chosen runtime types.
|
||||||
// For this it is necessary to specify a size, e.g. 32 bit.
|
// For this it is necessary to specify a size, e.g. 32 bit.
|
||||||
var var_int = 12345;
|
var var_int: u32 = 12345;
|
||||||
var var_float = 987.654;
|
var var_float: f32 = 987.654;
|
||||||
|
|
||||||
// We can change what is stored at the areas set aside for
|
// We can change what is stored at the areas set aside for
|
||||||
// "var_int" and "var_float" in the running compiled program.
|
// "var_int" and "var_float" in the running compiled program.
|
||||||
|
|
|
@ -35,7 +35,7 @@ pub fn main() void {
|
||||||
// In this contrived example, we've decided to allocate some
|
// In this contrived example, we've decided to allocate some
|
||||||
// arrays using a variable count! But something's missing...
|
// arrays using a variable count! But something's missing...
|
||||||
//
|
//
|
||||||
var count = 0;
|
comptime var count = 0;
|
||||||
|
|
||||||
count += 1;
|
count += 1;
|
||||||
const a1: [count]u8 = .{'A'} ** count;
|
const a1: [count]u8 = .{'A'} ** count;
|
||||||
|
|
|
@ -43,7 +43,8 @@ const Schooner = struct {
|
||||||
//
|
//
|
||||||
// Please change this so that it sets a 0 scale to 1
|
// Please change this so that it sets a 0 scale to 1
|
||||||
// instead.
|
// instead.
|
||||||
if (my_scale == 0) @compileError("Scale 1:0 is not valid!");
|
//if (my_scale == 0) @compileError("Scale 1:0 is not valid!");
|
||||||
|
if (my_scale == 0) my_scale = 1;
|
||||||
|
|
||||||
self.scale = my_scale;
|
self.scale = my_scale;
|
||||||
self.hull_length /= my_scale;
|
self.hull_length /= my_scale;
|
||||||
|
@ -69,7 +70,7 @@ pub fn main() void {
|
||||||
// Hey, we can't just pass this runtime variable as an
|
// Hey, we can't just pass this runtime variable as an
|
||||||
// argument to the scaleMe() method. What would let us do
|
// argument to the scaleMe() method. What would let us do
|
||||||
// that?
|
// that?
|
||||||
var scale: u32 = undefined;
|
comptime var scale: u32 = undefined;
|
||||||
|
|
||||||
scale = 32; // 1:32 scale
|
scale = 32; // 1:32 scale
|
||||||
|
|
||||||
|
|
|
@ -42,8 +42,8 @@ pub fn main() void {
|
||||||
// 2) Sets the size of the array of type T (which is the
|
// 2) Sets the size of the array of type T (which is the
|
||||||
// sequence we're creating and returning).
|
// sequence we're creating and returning).
|
||||||
//
|
//
|
||||||
fn makeSequence(comptime T: type, ??? size: usize) [???]T {
|
fn makeSequence(comptime T: type, comptime size: usize) [size]T {
|
||||||
var sequence: [???]T = undefined;
|
var sequence: [size]T = undefined;
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
|
|
||||||
while (i < size) : (i += 1) {
|
while (i < size) : (i += 1) {
|
||||||
|
|
|
@ -123,8 +123,8 @@ fn isADuck(possible_duck: anytype) bool {
|
||||||
// Please make sure MyType has both waddle() and quack()
|
// Please make sure MyType has both waddle() and quack()
|
||||||
// methods:
|
// methods:
|
||||||
const MyType = @TypeOf(possible_duck);
|
const MyType = @TypeOf(possible_duck);
|
||||||
const walks_like_duck = ???;
|
const walks_like_duck = @hasDecl(MyType, "waddle");
|
||||||
const quacks_like_duck = ???;
|
const quacks_like_duck = @hasDecl(MyType, "quack");
|
||||||
|
|
||||||
const is_duck = walks_like_duck and quacks_like_duck;
|
const is_duck = walks_like_duck and quacks_like_duck;
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ pub fn main() void {
|
||||||
|
|
||||||
const fields = @typeInfo(Narcissus).Struct.fields;
|
const fields = @typeInfo(Narcissus).Struct.fields;
|
||||||
|
|
||||||
??? {
|
inline for (fields) |field| {
|
||||||
if (field.type != void) {
|
if (field.type != void) {
|
||||||
print(" {s}", .{field.name});
|
print(" {s}", .{field.name});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue