-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Massive update to get images on pages, fix details, titles, ...
- Loading branch information
Showing
49 changed files
with
5,582 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Urgent: | ||
- [ ] Images sam | ||
- [ ] Finir la plannif | ||
- [ ] MVP | ||
- [ ] Epoll (ptetre lier ca a BD ?) | ||
- [ ] Changer le _index.org qui fait qu on arrive sur Utils | ||
|
||
N importe quand: | ||
- [ ] error.CeQueJeVeux | ||
- [ ] Corriger le feedback web et concu | ||
- [ ] tout relier et mettre Dans mes conclusion mettre des liens vers ma doc | ||
- [ ] Essayer export pdf avec les nouveaux *** | ||
- [ ] Noter tous les documents checkes et pas checks | ||
- [ ] Page accueil Hextra, avec gh, presentation, images, ... | ||
- [ ] Verifier que tous les premieres scripts sont exectuables | ||
- [ ] Un script pour lancer les serveurs se serait pas mal | ||
- [ ] https://zig.news/minosian/deciphering-many-item-types-the-skeleton-19i | ||
|
||
Fin: | ||
- [ ] sources zotero | ||
|
||
Peut etre: | ||
- [?] Websocket | ||
- [?] Zig and C TODO Add other languages that can use C code and make a graph with matplotlib | ||
- [?] CICD | ||
- [?] debug, binutils, breakpoints, debugger | ||
|
||
Questions: | ||
- Moyen que l expert regarde le site plutot que le document PDF pour la doc ? | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#+title: Errors | ||
#+weight: 3 | ||
|
||
** Errors | ||
In ZIG, [[https://ziglang.org/documentation/master/#Errors][errors]] are juste values. | ||
|
||
Those values are those of a special type of enum, that looks just like an enum but with the =error= keyword added. | ||
#+begin_src zig | ||
const TcpConnectionError = error{ | ||
HandshakeFailed, | ||
ConnectionClosed, | ||
UnexpectedData, | ||
}; | ||
#+end_src | ||
|
||
When you declare your enum using this keyword instead of =enum=, the compiler is going to know that those values are going to be errors, therefore they can be catched, tried and so on. | ||
|
||
If you want to return an error without having to make a whole enum for that, you can just return =error.MyPeronalError= like so. | ||
#+begin_src zig | ||
return error.MyPersonalError; | ||
#+end_src | ||
|
||
#+RESULTS: | ||
|
||
*** Handling errors in the application flow | ||
Functions can return either a value or an error, this is done by adding the =!= to the return type of a function | ||
With the =!= the return type of the function below is the coerced =anyerror!u8=, which means that it can either return the excepted value or an error. | ||
|
||
This way of handling errors is different from languages like Rust for exemple where you encapsulate the error or the value in a completly different type, the =Result= type. | ||
|
||
Note that since error sets are just enums, under the hood all the errors are just =u16= by default. | ||
|
||
It is important the the syntax of the type is =ErrorSet!Payload= and not =Payload!ErrorSet=. You first have to precise the error and then the payload, it is not possible to mix them up. | ||
|
||
When a function can return an error we have to either =try= or =catch= it. | ||
|
||
=try= is going to either get the result or the error. If your function contains a =try= statement, it has to return a =!T= type, where =T= it the type of the excepted value. | ||
|
||
=catch= is going to treat the error and return a value even if an error occured. | ||
|
||
#+begin_src zig :imports '(std) | ||
fn maybeErrorMaybeValue(isError: bool) !u8 { | ||
// ... might return an u8 or an error | ||
if (isError) { | ||
return error.Bar; | ||
} else { | ||
return 2; | ||
} | ||
} | ||
|
||
test "return value or error" { | ||
// "try" version | ||
const res = try maybeErrorMaybeValue(false); // if error, return the error | ||
try std.testing.expect(res == 2); | ||
|
||
// "catch" version | ||
const err = maybeErrorMaybeValue(true) catch 3; | ||
try std.testing.expect(err == 3); | ||
} | ||
|
||
test "should panic" { | ||
_ = maybeErrorMaybeValue(true) catch unreachable; | ||
} | ||
#+end_src | ||
|
||
*** anyerror | ||
This is the global error set containing all the possible errors that can happen in the current compilation unit. It is the superset of all the errors. | ||
|
||
This is the default error used when you just add the =!= withtout precsing the error type. Which means that =!u64= is just a syntaxic sugar for =anyerror!u64=. | ||
|
||
*** Errdefer | ||
This exemple is taken from the [[https://ziglang.org/documentation/master/#errdefer][official documentation]] | ||
[[https://ziglang.org/documentation/master/#errdefer][errdefer]] is a particularity of ZIG, it allows the user to execute some code when the function returns an error, it is useful exemple for deallocating variables you would have normally returned from the function, but since the function failed you deallocate that memory to avoid a memory leak. | ||
#+source: simple | ||
#+begin_src zig :imports '(std) | ||
fn createFoo(param: i32) !Foo { | ||
const foo = try tryToAllocateFoo(); | ||
// now we have allocated foo. we need to free it if the function fails. | ||
// but we want to return it if the function succeeds. | ||
errdefer deallocateFoo(foo); | ||
|
||
const tmp_buf = allocateTmpBuffer() orelse return error.OutOfMemory; | ||
// tmp_buf is truly a temporary resource, and we for sure want to clean it up | ||
// before this block leaves scope | ||
defer deallocateTmpBuffer(tmp_buf); | ||
|
||
if (param > 1337) return error.InvalidParam; | ||
|
||
// here the errdefer will not run since we're returning success from the function. | ||
// but the defer will run! | ||
return foo; | ||
} | ||
#+end_src | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.