You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Also, notice the `@import()` call at the first line. We use this built-in function
178
179
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
180
181
in C or C++, or, to the `import` statement in Python or Javascript code.
181
182
In this example, we are importing the `std` module,
182
183
which gives you access to the Zig Standard Library.
@@ -205,13 +206,13 @@ The function returns an integer of the type `i32` as result.
205
206
Zig is not exactly a strongly-typed language. Because you can (if you want to) omit
206
207
the type of an object in your code, if this type can be derived from the assigned value.
207
208
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,
209
210
the return type of every function you create in Zig. So, at least in function declarations,
210
211
Zig is a strongly-typed language.
211
212
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
213
214
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`,
215
216
which is a signed 32 bit integer. In this part,
216
217
the syntax in Zig is identical to the syntax in Rust, which also specifies types by
217
218
using the colon character.
@@ -222,46 +223,40 @@ a signed 32 bit integer (`i32`) value.
222
223
223
224
Notice that we also have an `export` keyword before the function declaration. This keyword
224
225
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.
231
229
If we removed the `export` keyword from the `add()` function declaration,
232
230
then, this function would be no longer exposed in the library object built
233
231
by the `zig` compiler.
234
232
235
233
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
-
241
234
### The `main.zig` file {#sec-main-file}
242
235
243
236
Now that we have learned a lot about Zig's syntax from the `root.zig` file,
244
237
let's take a look at the `main.zig` file.
245
238
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.
247
240
248
241
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
250
243
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
265
260
a *try catch* pattern, and Zig, this is no different. But, if we look at the `main()` function
266
261
below, you can see that we do have a `try` keyword in the 5th line. But we do not have a `catch` keyword
267
262
in this code.
@@ -272,28 +267,30 @@ we are not treating (or dealing with) this error.
272
267
So, if this expression do return an error, we are not catching and solving this error in any way.
273
268
That is why the exclamation mark was added to the return type of the function.
274
269
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
278
273
stack trace to `stderr`.
279
274
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
281
276
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
283
278
to stop the execution. You are obligated to face the error.
284
279
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.
287
284
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
291
286
will execute some code that fixes this error.
287
+
<!-- there are other options to check return values, eg. switch/case, ?, while, for, ... -->
292
288
293
-
So, at least for C programmers, they do need to write a lot of ifstatements 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
295
291
extraordinary for them. Because this `try` keyword can automatically unwrap the error,
296
292
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 :-) -->
297
294
298
295
299
296
```{zig}
@@ -307,22 +304,17 @@ pub fn main() !void {
307
304
}
308
305
```
309
306
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.
313
309
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.
316
311
Unless, you explicitly mark this function as a public function with the `pub` keyword.
317
312
This means that the `pub` keyword in Zig do essentially the opposite of what the `static` keyword
318
313
do in C/C++.
319
314
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.
326
318
327
319
### Compiling your source code {#sec-compile-code}
0 commit comments