-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/content/chapter3_data_types/lesson06_results/code.gleam
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,25 @@ | ||
import gleam/io | ||
import gleam/int | ||
|
||
pub fn main() { | ||
io.debug(buy_pastry(10)) | ||
io.debug(buy_pastry(8)) | ||
io.debug(buy_pastry(5)) | ||
io.debug(buy_pastry(3)) | ||
} | ||
|
||
pub type Error { | ||
NotEnoughMoney(required: Int) | ||
NotLuckyEnough | ||
} | ||
|
||
fn buy_pastry(money: Int) -> Result(Int, Error) { | ||
case money >= 5 { | ||
True -> | ||
case int.random(4) == 0 { | ||
True -> Error(NotLuckyEnough) | ||
False -> Ok(money - 5) | ||
} | ||
False -> Error(NotEnoughMoney(required: 5)) | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/content/chapter3_data_types/lesson06_results/text.html
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,37 @@ | ||
<p> | ||
Gleam doesn't use exceptions, instead computations that can either succeed or | ||
fail return a value of the built-in <code>Result(value, error)</code> type. It | ||
has two variants: | ||
</p> | ||
<ul> | ||
<li> | ||
<code>Ok</code>, which contains the return value of a successful | ||
computation. | ||
</li> | ||
<li> | ||
<code>Error</code>, which contains the reason for a failed computation. | ||
</li> | ||
</ul> | ||
<p> | ||
The type is generic with two type parameters, one for the success value and | ||
one for the error. With these the result can hold any type for success and | ||
failure. | ||
</p> | ||
<p> | ||
Commonly Gleam programs and library will define custom types with a variant | ||
for each possible problem that can arise, along with any error information | ||
that would be useful to the programmer. | ||
</p> | ||
<p> | ||
This is advantageous over exceptions as you can immediately see what if any | ||
errors a function can return, and the compiler will ensure they are handled. | ||
No nasty surprises with unexpected exceptions! | ||
</p> | ||
<p> | ||
Result value can be handled by pattern matching with a | ||
<code>case</code> expression, but given how frequently results are returned | ||
this can become unwieldy. Gleam code commonly uses the | ||
<code>gleam/result</code> standard library module and | ||
<code>use</code> expressions when working with results, both of which will be | ||
covered in later chapters. | ||
</p> |
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions
6
src/content/chapter4_standard_library/lesson00_standard_library_package/code.gleam
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,6 @@ | ||
import gleam/io | ||
|
||
pub fn main() { | ||
io.println("Hello, Joe!") | ||
io.println("Hello, Mike!") | ||
} |
14 changes: 14 additions & 0 deletions
14
src/content/chapter4_standard_library/lesson00_standard_library_package/text.html
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,14 @@ | ||
<p> | ||
The Gleam standard library is a regular Gleam package that has been published | ||
to the <a href="https://hex.pm">Hex</a> package repository. You could opt to | ||
not use if you wish, though almost all Gleam projects depend on it. | ||
</p> | ||
<p> | ||
All of the modules imported so far in this guide, such as | ||
<code>gleam/io</code>, are from the standard library. | ||
</p> | ||
<p> | ||
All of the documentation for the standard library is available on | ||
<a href="https://hexdocs.pm/gleam_stdlib/">HexDocs</a>. We will go over some | ||
of the most commonly used modules now. | ||
</p> |