Skip to content

Commit 9ad3aea

Browse files
authored
Merge pull request #73 from kurz-m/makurz-contribute
some minor typo fixes in 03-structs.qmd
2 parents afa7d12 + 048b2a0 commit 9ad3aea

File tree

7 files changed

+84
-74
lines changed

7 files changed

+84
-74
lines changed

Chapters/03-structs.qmd

+20-21
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ But also notice that every line in the struct body that describes a data member,
722722
So every time you declare a data member in your Zig code, always end the line with a comma character, instead
723723
of ending it with the traditional semicolon character (`;`).
724724

725-
Next, also notice in this example, that we have registrated an `init()` function as a method
725+
Next, we have registered an `init()` function as a method
726726
of this `User` struct. This `init()` method is the constructor method that we will use to instantiate
727727
every new `User` object. That is why this `init()` function returns a new `User` object as result.
728728

@@ -804,7 +804,7 @@ In essence, the `zig` compiler will look for some hint of what is the type of th
804804
This hint can be the type annotation of a function argument,
805805
or the return type annotation of the function that you are using, or the type annotation
806806
of an existing object.
807-
If the compiler do find such type annotation, then, it will use this
807+
If the compiler does find such type annotation, it will use this
808808
type in your literal struct.
809809

810810
Anonymous structs are very common to be used as inputs to function arguments in Zig.
@@ -857,14 +857,14 @@ const Vec3 = struct {
857857
### The `self` method argument {#sec-self-arg}
858858

859859
In every language that have OOP, when we declare a method of some class or struct, we
860-
usually declare this method as a function that have a `self` argument.
860+
usually declare this method as a function that has a `self` argument.
861861
This `self` argument is the reference to the object itself from which the method
862862
is being called from.
863863

864864
It is not mandatory to use this `self` argument. But why would you not use this `self` argument?
865865
There is no reason to not use it. Because the only way to get access to the data stored in the
866866
data members of your struct is to access them through this `self` argument.
867-
If you don't need to use the data in the data members of your struct inside your method, then, you very likely don't need
867+
If you don't need to use the data in the data members of your struct inside your method, you very likely don't need
868868
a method. You can just declare this logic as a simple function, outside of your
869869
struct declaration.
870870

@@ -939,12 +939,12 @@ As a result of that, when we create `Vec3` objects we usually create them as
939939
constant objects, like the `v1` and `v2` objects presented in @sec-self-arg.
940940
We can create them as variable objects with the `var` keyword,
941941
if we want to. But because the methods of this `Vec3` struct do not change
942-
the state of the objects in any point, is unnecessary to mark them
942+
the state of the objects in any point, it's unnecessary to mark them
943943
as variable objects.
944944

945-
But why? Why am I talkin about this here? Is because the `self` argument
945+
But why? Why am I talking about this here? It's because the `self` argument
946946
in the methods is affected depending on whether the
947-
methods present in a struct change or not the state of the object itself.
947+
methods present in a struct change or don't change the state of the object itself.
948948
More specifically, when you have a method in a struct that changes the state
949949
of the object (i.e. change the value of a data member), the `self` argument
950950
in this method must be annotated in a different manner.
@@ -997,8 +997,8 @@ const Vec3 = struct {
997997
```
998998

999999
Notice in the code example above that we have added a new method
1000-
to our `Vec3` struct named `double()`. This method essentially doubles the
1001-
coordinate values of our vector object. Also notice that, in the
1000+
to our `Vec3` struct named `double()`. This method doubles the
1001+
coordinate values of our vector object. In the
10021002
case of the `double()` method, we annotated the `self` argument as `*Vec3`,
10031003
indicating that this argument receives a pointer (or a reference, if you prefer to call it this way)
10041004
to a `Vec3` object as input.
@@ -1020,7 +1020,7 @@ Doubled: 8.4
10201020

10211021
Now, if you change the `self` argument in this `double()` method to `self: Vec3`, like in the
10221022
`distance()` method, you will get the compiler error exposed below as result. Notice that this
1023-
error message is indicating a line from the `double()` method body,
1023+
error message is showing a line from the `double()` method body,
10241024
indicating that you cannot alter the value of the `x` data member.
10251025

10261026
```{zig}
@@ -1043,15 +1043,14 @@ If you take some time, and think hard about this error message, you will underst
10431043
You already have the tools to understand why we are getting this error message.
10441044
We have talked about it already in @sec-fun-pars.
10451045
So remember, every function argument is immutable in Zig, and `self`
1046-
is included in this rule.
1046+
is no exception to this rule.
10471047

1048-
It does not matter if the object that you pass as input to the function argument is
1049-
a variable object or not. In this example, we marked the `v3` object as a variable object.
1048+
In this example, we marked the `v3` object as a variable object.
10501049
But this does not matter. Because it is not about the input object, it is about
10511050
the function argument.
10521051

10531052
The problem begins when we try to alter the value of `self` directly, which is a function argument,
1054-
and, every function argument is immutable by default. You may quest yourself how can we overcome
1053+
and, every function argument is immutable by default. You may ask yourself how can we overcome
10551054
this barrier, and once again, the solution was also discussed in @sec-fun-pars.
10561055
We overcome this barrier, by explicitly marking the `self` argument as a pointer.
10571056

@@ -1083,17 +1082,17 @@ as input.
10831082
In general, type inference in Zig is done by using the dot character (`.`).
10841083
Everytime you see a dot character written before a struct literal, or before an enum value, or something like that,
10851084
you know that this dot character is playing a special party in this place. More specifically, it is
1086-
telling the `zig` compiler something on the lines of: "Hey! Can you infer the type of this
1087-
value for me? Please!". In other words, this dot character is playing a role similar to the `auto` keyword in C++.
1085+
telling the `zig` compiler something along the lines of: "Hey! Can you infer the type of this
1086+
value for me? Please!". In other words, this dot character is playing a similar role as the `auto` keyword in C++.
10881087

1089-
I give you some examples of this in @sec-anonymous-struct-literals, where we present anonymous struct literals.
1090-
Anonymous struct literals are, essentially, struct literals that use type inference to
1088+
I gave you some examples of this in @sec-anonymous-struct-literals, where we used anonymous struct literals.
1089+
Anonymous struct literals are, struct literals that use type inference to
10911090
infer the exact type of this particular struct literal.
10921091
This type inference is done by looking for some minimal hint of the correct data type to be used.
10931092
You could say that the `zig` compiler looks for any neighbouring type annotation that might tell him
10941093
what would be the correct type.
10951094

1096-
Another common place where we use type inference in Zig is at switch statements (which we talk about in @sec-switch).
1095+
Another common place where we use type inference in Zig is at switch statements (which we talked about in @sec-switch).
10971096
I also gave some other examples of type inference in @sec-switch, where we were inferring the data types of enum values listed inside
10981097
of switch statements (e.g. `.DE`).
10991098
But as another example, take a look at this `fence()` function reproduced below,
@@ -1103,14 +1102,14 @@ of the Zig Standard Library.
11031102
[^fence-fn]: <https://github.com/ziglang/zig/blob/master/lib/std/atomic.zig>.
11041103

11051104
There are a lot of things in this function that we haven't talked about yet, such as:
1106-
what `comptime` means? `inline`? `extern`? What is this star symbol before `Self`?
1105+
what `comptime` means? `inline`? `extern`?
11071106
Let's just ignore all of these things, and focus solely on the switch statement
11081107
that is inside this function.
11091108

11101109
We can see that this switch statement uses the `order` object as input. This `order`
11111110
object is one of the inputs of this `fence()` function, and we can see in the type annotation,
11121111
that this object is of type `AtomicOrder`. We can also see a bunch of values inside the
1113-
switch statements that begins with a dot character, such as `.release` and `.acquire`.
1112+
switch statements that begin with a dot character, such as `.release` and `.acquire`.
11141113

11151114
Because these weird values contain a dot character before them, we are asking the `zig`
11161115
compiler to infer the types of these values inside the switch statement. Then, the `zig`

_freeze/Chapters/03-structs/execute-results/html.json

+2-2
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)