Skip to content

Commit bfcf18c

Browse files
committed
langref: delete misleading example code about packed structs
1 parent 4fc295d commit bfcf18c

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

doc/langref/test_structs.zig

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,13 @@ const Point = struct {
66
y: f32,
77
};
88

9-
// Maybe we want to pass it to OpenGL so we want to be particular about
10-
// how the bytes are arranged.
11-
const Point2 = packed struct {
12-
x: f32,
13-
y: f32,
14-
};
15-
169
// Declare an instance of a struct.
17-
const p = Point{
10+
const p: Point = .{
1811
.x = 0.12,
1912
.y = 0.34,
2013
};
2114

22-
// Maybe we're not ready to fill out some of the fields.
23-
var p2 = Point{
24-
.x = 0.12,
25-
.y = undefined,
26-
};
27-
28-
// Structs can have methods
29-
// Struct methods are not special, they are only namespaced
30-
// functions that you can call with dot syntax.
15+
// Functions in the struct's namespace can be called with dot syntax.
3116
const Vec3 = struct {
3217
x: f32,
3318
y: f32,
@@ -46,7 +31,6 @@ const Vec3 = struct {
4631
}
4732
};
4833

49-
const expect = @import("std").testing.expect;
5034
test "dot product" {
5135
const v1 = Vec3.init(1.0, 0.0, 0.0);
5236
const v2 = Vec3.init(0.0, 1.0, 0.0);
@@ -67,14 +51,14 @@ test "struct namespaced variable" {
6751
try expect(Empty.PI == 3.14);
6852
try expect(@sizeOf(Empty) == 0);
6953

70-
// you can still instantiate an empty struct
71-
const does_nothing = Empty{};
54+
// Empty structs can be instantiated the same as usual.
55+
const does_nothing: Empty = .{};
7256

7357
_ = does_nothing;
7458
}
7559

76-
// struct field order is determined by the compiler for optimal performance.
77-
// however, you can still calculate a struct base pointer given a field pointer:
60+
// Struct field order is determined by the compiler, however, a base pointer
61+
// can be computed from a field pointer:
7862
fn setYBasedOnX(x: *f32, y: f32) void {
7963
const point: *Point = @fieldParentPtr("x", x);
8064
point.y = y;
@@ -88,8 +72,7 @@ test "field parent pointer" {
8872
try expect(point.y == 0.9);
8973
}
9074

91-
// You can return a struct from a function. This is how we do generics
92-
// in Zig:
75+
// Structs can be returned from functions.
9376
fn LinkedList(comptime T: type) type {
9477
return struct {
9578
pub const Node = struct {
@@ -105,8 +88,7 @@ fn LinkedList(comptime T: type) type {
10588
}
10689

10790
test "linked list" {
108-
// Functions called at compile-time are memoized. This means you can
109-
// do this:
91+
// Functions called at compile-time are memoized.
11092
try expect(LinkedList(i32) == LinkedList(i32));
11193

11294
const list = LinkedList(i32){
@@ -139,4 +121,6 @@ test "linked list" {
139121
// instead of try expect(list2.first.?.*.data == 1234);
140122
}
141123

124+
const expect = @import("std").testing.expect;
125+
142126
// test

0 commit comments

Comments
 (0)