Skip to content

Commit c9ffa78

Browse files
authored
Merge pull request #41 from sroller/Typos
Typos and inconsistencies
2 parents dc8ce74 + 3ebde66 commit c9ffa78

File tree

4 files changed

+91
-95
lines changed

4 files changed

+91
-95
lines changed

Chapters/01-zig-weird.qmd

+57-65
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ understanding and exploring the exciting world of Zig.
2727

2828
I assume you have previous experience with some programming
2929
language in this book, not necessarily with a low-level one.
30-
So, if you have experience with Python, or Javascript, for example, is fine.
30+
So, if you have experience with Python, or Javascript, for example, it will be fine.
3131
But, if you do have experience with low-level languages, such as C, C++, or
3232
Rust, you will probably learn faster throughout this book.
3333

@@ -49,19 +49,19 @@ This philosophy becomes clear with the following phrase from the official websit
4949
5050
This phrase is specially true for C++ programmers. Because C++ is a gigantic language,
5151
with tons of features, and also, there are lots of different "flavors of C++". These elements
52-
are what makes C++ so much complex and hard to learn. Zig tries to go in the opposite direction.
52+
are what makes C++ so complex and hard to learn. Zig tries to go in the opposite direction.
5353
Zig is a very simple language, more closely related to other simple languages such as C and Go.
5454

5555
The phrase above is still important for C programmers too. Because, even C being a simple
5656
language, it is still hard sometimes to read and understand C code. For example, pre-processor macros in
57-
C are an evil source of confusion. They really makes it hard sometimes to debug
57+
C are a frequent source of confusion. They really make it sometimes hard to debug
5858
C programs. Because macros are essentially a second language embedded in C that obscures
5959
your C code. With macros, you are no longer 100% sure about which pieces
60-
of code are being sent to the compiler. It obscures the actual source code that you wrote.
60+
of the code are being sent to the compiler, i.e.
61+
they obscures the actual source code that you wrote.
6162

6263
You don't have macros in Zig. In Zig, the code you write, is the actual code that get's compiled by the compiler.
63-
You don't have evil features that obscures you code.
64-
You also don't have hidden control flow happening behind the scenes. And, you also
64+
You also don't have a hidden control flow happening behind the scenes. And, you also
6565
don't have functions or operators from the standard library that make
6666
hidden memory allocations behind your back.
6767

@@ -127,7 +127,8 @@ The `ìnit` command also creates two additional files in our working directory:
127127
This script is executed when you call the `build` command from the `zig` compiler.
128128
In other words, this file contain Zig code that executes the necessary steps to build the entire project.
129129

130-
In general, low-level languages normally use a compiler to build your
130+
131+
Low-level languages normally use a compiler to build your
131132
source code into binary executables or binary libraries.
132133
Nevertheless, this process of compiling your source code and building
133134
binary executables or binary libraries from it, became a real challenge
@@ -147,9 +148,9 @@ As a result, in C/C++ projects, you have not only to install and
147148
manage your C/C++ compilers, but you also have to install and manage
148149
these build systems separately.
149150

150-
But instead of using a separate build system, in Zig, we use the
151-
Zig language itself to write build scripts.
152-
In other words, Zig contains a native build system in it. And
151+
In Zig, we don't need to use a separate set of tools to build our projects,
152+
because a build system is embedded inside the language itself.
153+
Therefore, Zig contains a native build system in it, and
153154
we can use this build system to write small scripts in Zig,
154155
which describes the necessary steps to build/compile our Zig project[^zig-build-system].
155156
So, everything you need to build a complex Zig project is the
@@ -169,14 +170,14 @@ or the `Cargo.toml` file in Rust projects.
169170
### The file `root.zig` {#sec-root-file}
170171

171172
Let's take a look into the `root.zig` file.
172-
You might have notice that every line of code with an expression ends with a semicolon (`;`).
173+
You might have noticed that every line of code with an expression ends with a semicolon (`;`).
173174
This follows the syntax of a C-family programming language[^c-family].
174175

175176
[^c-family]: <https://en.wikipedia.org/wiki/List_of_C-family_programming_languages>
176177

177178
Also, notice the `@import()` call at the first line. We use this built-in function
178179
to import functionality from other Zig modules into our current module.
179-
In other words, this `@import()` function works similarly to the `#include` pre-processor
180+
This `@import()` function works similarly to the `#include` pre-processor
180181
in C or C++, or, to the `import` statement in Python or Javascript code.
181182
In this example, we are importing the `std` module,
182183
which gives you access to the Zig Standard Library.
@@ -205,13 +206,13 @@ The function returns an integer of the type `i32` as result.
205206
Zig is not exactly a strongly-typed language. Because you can (if you want to) omit
206207
the type of an object in your code, if this type can be derived from the assigned value.
207208
But there are other situations where you do need to be explicit.
208-
For example, you do have to explicitly specify the type of every single function argument, and also,
209+
For example, you do have to explicitly specify the type of each function argument, and also,
209210
the return type of every function you create in Zig. So, at least in function declarations,
210211
Zig is a strongly-typed language.
211212

212-
We specify the type of an object or a function argument in Zig, by
213+
We specify the type of an object or a function argument in Zig by
213214
using a colon character (`:`) followed by the type after the name of this object/function argument.
214-
With the expressions `a: i32` and `b: i32`, we know that, both `a` and `b` arguments have type `i32`,
215+
With the expressions `a: i32` and `b: i32`, we know that both `a` and `b` arguments have type `i32`,
215216
which is a signed 32 bit integer. In this part,
216217
the syntax in Zig is identical to the syntax in Rust, which also specifies types by
217218
using the colon character.
@@ -222,46 +223,40 @@ a signed 32 bit integer (`i32`) value.
222223

223224
Notice that we also have an `export` keyword before the function declaration. This keyword
224225
is similar to the `extern` keyword in C. It exposes the function
225-
to make it available in the library API.
226-
227-
228-
In other words, if you have a project where you are currently building
229-
a library for other people to use, you need to expose your functions
230-
so that they are available in the library's API, so that users can use it.
226+
to make it available in the library API. Therefore, if you are writing
227+
a library for other people to use, you have to expose the functions
228+
you write in the public API of this library by using this `export` keyword.
231229
If we removed the `export` keyword from the `add()` function declaration,
232230
then, this function would be no longer exposed in the library object built
233231
by the `zig` compiler.
234232

235233

236-
Having that in mind, the keyword `export` is a keyword used in libraries written in Zig.
237-
If you are not currently writing a library in your project, then, you do not need to
238-
care about this keyword.
239-
240-
241234
### The `main.zig` file {#sec-main-file}
242235

243236
Now that we have learned a lot about Zig's syntax from the `root.zig` file,
244237
let's take a look at the `main.zig` file.
245238
A lot of the elements we saw in `root.zig` are also present in `main.zig`.
246-
But we have some other elements that we did not have seen yet, so let's dive in.
239+
But there are some other elements that we haven't seen yet, so let's dive in.
247240

248241
First, look at the return type of the `main()` function in this file.
249-
We can see a small change. Now, the return
242+
We can see a small change. The return
250243
type of the function (`void`) is accompanied by an exclamation mark (`!`).
251-
What this exclamation mark is telling us, is that this `main()` function
252-
might also return an error.
253-
254-
So, in this example, the `main()` function can either return `void`, or, return an error.
255-
This is an interesting feature of Zig. If you write a function, and, something inside of
256-
the body of this function might return an error, then, you are forced to:
257-
258-
- either add the exclamation mark to the return type of the function, to make it clear that
259-
this function might return an error.
260-
- or explicitly handle this error that might occur inside the function, to make sure that,
261-
if this error does happen, you are prepared, and your function will no longer return an error
262-
because you handled the error inside your function.
263-
264-
In most programming languages, we normally handle (or deals with) an error through
244+
This exclamation mark tells us that this `main()` function
245+
might return an error.
246+
247+
In this example, the `main()` function can either return `void` or return an error.
248+
This is an interesting feature of Zig. If you write a function and something inside of
249+
the body of this function might return an error then you are forced to:
250+
251+
- either add the exclamation mark to the return type of the function and make it clear that
252+
this function might return an error
253+
- explicitly handle this error inside the function
254+
255+
<!-- is it different in Zig or not?
256+
C++ has try/catch
257+
Zig has try if i understand correctly. is there a catch somewhere?
258+
I don't understand what's said in the follwing paragraphs. -->
259+
In most programming languages, we normally handle (or deal with) an error through
265260
a *try catch* pattern, and Zig, this is no different. But, if we look at the `main()` function
266261
below, you can see that we do have a `try` keyword in the 5th line. But we do not have a `catch` keyword
267262
in this code.
@@ -272,28 +267,30 @@ we are not treating (or dealing with) this error.
272267
So, if this expression do return an error, we are not catching and solving this error in any way.
273268
That is why the exclamation mark was added to the return type of the function.
274269

275-
So, in essence, the `try` keyword executes the expression `stdout.print()`. If this expression
276-
returns a valid value, then, the `try` keyword do nothing essentially. It simply passes this value forward. But, if the expression do
277-
return an error, then, the `try` keyword will unwrap and return this error from the function, and also print it's
270+
The `try` keyword executes the expression `stdout.print()`. If this expression
271+
returns a valid value then the `try` keyword do nothing. It only passes the value forward. But if the expression does
272+
return an error then the `try` keyword will unwrap <!-- the error? -->and return this error from the function and also print its
278273
stack trace to `stderr`.
279274

280-
This might sound weird to you, if you come from a high-level language. Because in
275+
This might sound weird to you if you come from a high-level language. Because in
281276
high-level languages, such as Python, if an error occurs somewhere, this error is automatically
282-
returned and the execution of your program will automatically stops, even if you don't want
277+
returned and the execution of your program will automatically stop even if you don't want
283278
to stop the execution. You are obligated to face the error.
284279

285-
But if you come from a low-level language, then, maybe, this idea do not sound so weird or distant to you.
286-
Because in C for example, normally functions doesn't raise errors, or, they normally don't stop the execution.
280+
<!-- confusing explanation about best practices of error handling in C. As C programmer I might disagree
281+
but it has nothing to do with Zig. just confuses the reader -->
282+
But if you come from a low-level language then this idea doesn't sound so weird to you.
283+
Because in C for example, functions don't raise errors and they don't stop the execution.
287284
In C, error handling
288-
is done by constantly checking the return value of the function. So, you run the function,
289-
and then, you use an if statement to check if the function returned a value that is valid,
290-
or, if it returned an error. If an error was returned from the function, then, the if statement
285+
is done by checking the return value of the function. You run the function, then you use an if statement to check if the function returned a value that is valid if it returned an error. If an error was returned from the function then the if statement
291286
will execute some code that fixes this error.
287+
<!-- there are other options to check return values, eg. switch/case, ?, while, for, ... -->
292288

293-
So, at least for C programmers, they do need to write a lot of if statements to
294-
constantly check for errors around their code. And because of that, this simple feature from Zig, might be
289+
C programmers must write a lot of if-statements
290+
checking for errors around their code. And because of that, this simple feature from Zig, might be
295291
extraordinary for them. Because this `try` keyword can automatically unwrap the error,
296292
and warn you about this error, and let you deal with it, without any extra work from the programmer.
293+
<!-- i'm looking forward to learn more about Zig because what you wrote doesn't sound convincing :-) -->
297294

298295

299296
```{zig}
@@ -307,22 +304,17 @@ pub fn main() !void {
307304
}
308305
```
309306

310-
Now, another thing that you might have noticed in this code example, is that
311-
the `main()` function is marked with the `pub` keyword. This keyword means
312-
"public". It marks the `main()` function as a *public function* from this module.
307+
Another thing that you might have noticed in this code example, is that
308+
the `main()` function is marked with the `pub` keyword. It marks the `main()` function as a *public function* from this module.
313309

314-
In other words, every function that you declare in your Zig module is, by default, a private (or "static")
315-
function that belongs to this Zig module, and can only be used (or called) from within this same module.
310+
Every function in your Zig module is by default private to this Zig module and can only be called from within the module.
316311
Unless, you explicitly mark this function as a public function with the `pub` keyword.
317312
This means that the `pub` keyword in Zig do essentially the opposite of what the `static` keyword
318313
do in C/C++.
319314

320-
By making a function "public", you allow other Zig modules to access and call this function,
321-
and use it for they own purposes.
322-
all these other Zig modules need to do is, to import your module with the `@import()`
323-
built-in function. Then, they get access to all public functions that are present in
324-
your Zig module.
325-
315+
By making a function "public" you allow other Zig modules to access and call it.
316+
A calling Zig module imports the module with the `@import()`
317+
built-in. That makes all public functions from the imported module visible.
326318

327319
### Compiling your source code {#sec-compile-code}
328320

_freeze/Chapters/01-zig-weird/execute-results/html.json

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

0 commit comments

Comments
 (0)