@@ -722,7 +722,7 @@ But also notice that every line in the struct body that describes a data member,
722
722
So every time you declare a data member in your Zig code, always end the line with a comma character, instead
723
723
of ending it with the traditional semicolon character (` ; ` ).
724
724
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
726
726
of this ` User ` struct. This ` init() ` method is the constructor method that we will use to instantiate
727
727
every new ` User ` object. That is why this ` init() ` function returns a new ` User ` object as result.
728
728
@@ -804,7 +804,7 @@ In essence, the `zig` compiler will look for some hint of what is the type of th
804
804
This hint can be the type annotation of a function argument,
805
805
or the return type annotation of the function that you are using, or the type annotation
806
806
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
808
808
type in your literal struct.
809
809
810
810
Anonymous structs are very common to be used as inputs to function arguments in Zig.
@@ -857,14 +857,14 @@ const Vec3 = struct {
857
857
### The ` self ` method argument {#sec-self-arg}
858
858
859
859
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.
861
861
This ` self ` argument is the reference to the object itself from which the method
862
862
is being called from.
863
863
864
864
It is not mandatory to use this ` self ` argument. But why would you not use this ` self ` argument?
865
865
There is no reason to not use it. Because the only way to get access to the data stored in the
866
866
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
868
868
a method. You can just declare this logic as a simple function, outside of your
869
869
struct declaration.
870
870
@@ -939,12 +939,12 @@ As a result of that, when we create `Vec3` objects we usually create them as
939
939
constant objects, like the ` v1 ` and ` v2 ` objects presented in @sec-self-arg .
940
940
We can create them as variable objects with the ` var ` keyword,
941
941
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
943
943
as variable objects.
944
944
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
946
946
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.
948
948
More specifically, when you have a method in a struct that changes the state
949
949
of the object (i.e. change the value of a data member), the ` self ` argument
950
950
in this method must be annotated in a different manner.
@@ -997,8 +997,8 @@ const Vec3 = struct {
997
997
```
998
998
999
999
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
1002
1002
case of the ` double() ` method, we annotated the ` self ` argument as ` *Vec3 ` ,
1003
1003
indicating that this argument receives a pointer (or a reference, if you prefer to call it this way)
1004
1004
to a ` Vec3 ` object as input.
@@ -1020,7 +1020,7 @@ Doubled: 8.4
1020
1020
1021
1021
Now, if you change the ` self ` argument in this ` double() ` method to ` self: Vec3 ` , like in the
1022
1022
` 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,
1024
1024
indicating that you cannot alter the value of the ` x ` data member.
1025
1025
1026
1026
``` {zig}
@@ -1043,15 +1043,14 @@ If you take some time, and think hard about this error message, you will underst
1043
1043
You already have the tools to understand why we are getting this error message.
1044
1044
We have talked about it already in @sec-fun-pars .
1045
1045
So remember, every function argument is immutable in Zig, and ` self `
1046
- is included in this rule.
1046
+ is no exception to this rule.
1047
1047
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.
1050
1049
But this does not matter. Because it is not about the input object, it is about
1051
1050
the function argument.
1052
1051
1053
1052
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
1055
1054
this barrier, and once again, the solution was also discussed in @sec-fun-pars .
1056
1055
We overcome this barrier, by explicitly marking the ` self ` argument as a pointer.
1057
1056
@@ -1083,17 +1082,17 @@ as input.
1083
1082
In general, type inference in Zig is done by using the dot character (` . ` ).
1084
1083
Everytime you see a dot character written before a struct literal, or before an enum value, or something like that,
1085
1084
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++.
1088
1087
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
1091
1090
infer the exact type of this particular struct literal.
1092
1091
This type inference is done by looking for some minimal hint of the correct data type to be used.
1093
1092
You could say that the ` zig ` compiler looks for any neighbouring type annotation that might tell him
1094
1093
what would be the correct type.
1095
1094
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 ).
1097
1096
I also gave some other examples of type inference in @sec-switch , where we were inferring the data types of enum values listed inside
1098
1097
of switch statements (e.g. ` .DE ` ).
1099
1098
But as another example, take a look at this ` fence() ` function reproduced below,
@@ -1103,14 +1102,14 @@ of the Zig Standard Library.
1103
1102
[ ^ fence-fn ] : < https://github.com/ziglang/zig/blob/master/lib/std/atomic.zig > .
1104
1103
1105
1104
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 ` ?
1107
1106
Let's just ignore all of these things, and focus solely on the switch statement
1108
1107
that is inside this function.
1109
1108
1110
1109
We can see that this switch statement uses the ` order ` object as input. This ` order `
1111
1110
object is one of the inputs of this ` fence() ` function, and we can see in the type annotation,
1112
1111
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 ` .
1114
1113
1115
1114
Because these weird values contain a dot character before them, we are asking the ` zig `
1116
1115
compiler to infer the types of these values inside the switch statement. Then, the ` zig `
0 commit comments