Skip to content

Commit

Permalink
Rename methods used for unwrapping values
Browse files Browse the repository at this point in the history
The renames are as follows:

- "unwrap" is now "get"
- "unwrap_or" is now "or"
- "unwrap_or_else" is now "or_else"
- "expect" is now "or_panic"

These methods are renamed to make the method names a bit shorter, remove
the need for sticking "unwrap" in the names, and because "unwrapping"
isn't really clearly defined in the context of Option and Result types
(especially Result as both Ok and Error can be unwrapped).

This fixes #662.

Changelog: changed
  • Loading branch information
yorickpeterse committed Feb 23, 2024
1 parent f729c2e commit 0536135
Show file tree
Hide file tree
Showing 28 changed files with 618 additions and 618 deletions.
4 changes: 2 additions & 2 deletions docs/source/getting-started/concurrency.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ class async Main {
fn async main {
let server = recover TcpServer
.new(IpAddress.v4(127, 0, 0, 1), port: 40_000)
.unwrap
.get
let client = recover server.accept.unwrap
let client = recover server.accept.get
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion docs/source/getting-started/control-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ The `throw` keyword is only available in methods of which the return type is a
## try

`try` takes an expression of which the type is either `std.result.Result` or
`std.option.Option`, and unwraps it. If the value is a `Result.Error` or an
`std.option.Option`, and gets it. If the value is a `Result.Error` or an
`Option.None`, the value is returned as-is.

Consider this example of using `try` with an `Option` value:
Expand Down
24 changes: 12 additions & 12 deletions docs/source/getting-started/files.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import std.stdio.STDOUT
class async Main {
fn async main {
let out = STDOUT.new
let file = ReadWriteFile.new('hello.txt').unwrap
let file = ReadWriteFile.new('hello.txt').get
let bytes = ByteArray.new
file.write_string('Hello, world!').unwrap
file.seek(0).unwrap
file.read_all(bytes).unwrap
out.write_bytes(bytes).unwrap
file.write_string('Hello, world!').get
file.seek(0).get
file.read_all(bytes).get
out.write_bytes(bytes).get
}
}
```
Expand All @@ -43,7 +43,7 @@ We used `ReadWriteFile` to open a file for both reading and writing, using
the cursor to the start of the file, then read the data back, and write it to
the terminal.

For the sake of brevity we've ignored error handling by using `unwrap`,
For the sake of brevity we've ignored error handling by using `get`,
resulting in the program terminating in the event of an error. Of course in a
real program you'll want more fine-grained error handling, but for the sake of
brevity we'll pretend our program won't produce any errors.
Expand All @@ -62,11 +62,11 @@ import std.stdio.STDOUT
class async Main {
fn async main {
let out = STDOUT.new
let file = ReadOnlyFile.new('hello.txt').unwrap
let file = ReadOnlyFile.new('hello.txt').get
let bytes = ByteArray.new
file.read_all(bytes).unwrap
out.write_bytes(bytes).unwrap
file.read_all(bytes).get
out.write_bytes(bytes).get
}
}
```
Expand All @@ -78,9 +78,9 @@ an error such as this:
```
Stack trace (the most recent call comes last):
[...]/files.inko:7 in main.Main.main
[...]/std/src/std/result.inko:119 in std.result.Result.unwrap
[...]/std/src/std/result.inko:119 in std.result.Result.get
[...]/std/src/std/process.inko:15 in std.process.panic
Process 'Main' (0x5645bdf31740) panicked: Result.unwrap can't unwrap an Error
Process 'Main' (0x5645bdf31740) panicked: Result.get expects an Ok(_), but an Error(_) is found
```

## Write-only files
Expand All @@ -92,7 +92,7 @@ import std.fs.file.WriteOnlyFile
class async Main {
fn async main {
let file = WriteOnlyFile.new('hello.txt').unwrap
let file = WriteOnlyFile.new('hello.txt').get
file.write_string('Hello, world!')
}
Expand Down
24 changes: 12 additions & 12 deletions docs/source/getting-started/hello-error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import std.stdio.STDOUT
class async Main {
fn async main {
STDOUT.new.print('Hello, world!').unwrap
STDOUT.new.print('Hello, world!').get
}
}
```

The change made here compared to the previous tutorial is the addition of
`.unwrap` at the end of the `print()` line. We'll look into what this does
later. Now run the program as follows:
The change made here compared to the previous tutorial is the addition of `.get`
at the end of the `print()` line. We'll look into what this does later. Now run
the program as follows:

```bash
inko run hello.inko | true
Expand All @@ -34,16 +34,16 @@ The output of this program should be something similar to the following:
```
Stack trace (the most recent call comes last):
[...]/hello.inko:5 in main.Main.main
[...]/std/src/std/result.inko:119 in std.result.Result.unwrap
[...]/std/src/std/result.inko:119 in std.result.Result.get
[...]/std/src/std/process.inko:15 in std.process.panic
Process 'Main' (0x5637caa83150) panicked: Result.unwrap can't unwrap an Error
Process 'Main' (0x5637caa83150) panicked: Result.get expects an Ok(_), but an Error(_) is found
```

What happened here is that the `print()` failed to write to the standard output
stream. The `unwrap` method call turns such an error into a "panic". A panic is
a type of error that terminates the program. Panics are the result of bugs in
your code, and as such can't be handled at runtime. In a well written program,
such errors shouldn't occur.
stream. The `get` method call turns such an error into a "panic". A panic is a
type of error that terminates the program. Panics are the result of bugs in your
code, and as such can't be handled at runtime. In a well written program, such
errors shouldn't occur.

## Handling the error

Expand Down Expand Up @@ -81,8 +81,8 @@ If all went well, the program should run _without_ printing anything to the
terminal.

This may seem surprising at first, but is indeed the correct behaviour: the
`print()` call still fails, but instead of calling `unwrap` we assign the result
to the variable `_`. By assigning the result of `print()` to `_`, the compiler
`print()` call still fails, but instead of calling `get` we assign the result to
the variable `_`. By assigning the result of `print()` to `_`, the compiler
won't emit any warnings because the result isn't used, nor will it emit any
warnings because the variable assigned to isn't used.

Expand Down
42 changes: 21 additions & 21 deletions docs/source/getting-started/sockets.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ import std.stdio.STDOUT
class async Main {
fn async main {
let stdout = STDOUT.new
let server = UdpSocket.new(IpAddress.v4(0, 0, 0, 0), port: 0).unwrap
let client = UdpSocket.new(IpAddress.v4(0, 0, 0, 0), port: 0).unwrap
let addr = server.local_address.unwrap
let server = UdpSocket.new(IpAddress.v4(0, 0, 0, 0), port: 0).get
let client = UdpSocket.new(IpAddress.v4(0, 0, 0, 0), port: 0).get
let addr = server.local_address.get
client.connect(addr.ip.unwrap, addr.port).unwrap
client.write_string('Hello, world!').unwrap
client.connect(addr.ip.get, addr.port).get
client.write_string('Hello, world!').get
let bytes = ByteArray.new
server.read(into: bytes, size: 32).unwrap
stdout.write_bytes(bytes).unwrap
server.read(into: bytes, size: 32).get
stdout.write_bytes(bytes).get
}
}
```
Expand All @@ -52,8 +52,8 @@ possible.
Our sockets are created as follows:

```inko
let server = UdpSocket.new(IpAddress.v4(0, 0, 0, 0), port: 0).unwrap
let client = UdpSocket.new(IpAddress.v4(0, 0, 0, 0), port: 0).unwrap
let server = UdpSocket.new(IpAddress.v4(0, 0, 0, 0), port: 0).get
let client = UdpSocket.new(IpAddress.v4(0, 0, 0, 0), port: 0).get
```

What happens here is that we create two sockets that bind themselves to IP
Expand All @@ -64,10 +64,10 @@ worry about using a port that's already in use.
Next, we encounter the following:

```inko
let addr = server.local_address.unwrap
let addr = server.local_address.get
client.connect(addr.ip.unwrap, addr.port).unwrap
client.write_string('Hello, world!').unwrap
client.connect(addr.ip.get, addr.port).get
client.write_string('Hello, world!').get
```

Here we get the address of the server we need to connect the client to, which we
Expand All @@ -79,14 +79,14 @@ We then read the data back from the server:
```inko
let bytes = ByteArray.new
server.read(into: bytes, size: 32).unwrap
stdout.write_bytes(bytes).unwrap
server.read(into: bytes, size: 32).get
stdout.write_bytes(bytes).get
```

When using sockets you shouldn't use `read_all` as we did in the files tutorial,
because `read_all` won't return until the socket is disconnected.

Just as in the files tutorial, we use `unwrap` to handle errors for the sake of
Just as in the files tutorial, we use `get` to handle errors for the sake of
brevity.

## Using TCP sockets
Expand All @@ -102,12 +102,12 @@ import std.stdio.STDOUT
class async Main {
fn async main {
let stdout = STDOUT.new
let server = TcpServer.new(IpAddress.v4(0, 0, 0, 0), port: 9999).unwrap
let client = server.accept.unwrap
let server = TcpServer.new(IpAddress.v4(0, 0, 0, 0), port: 9999).get
let client = server.accept.get
let bytes = ByteArray.new
client.read(into: bytes, size: 32).unwrap
stdout.write_bytes(bytes).unwrap
client.read(into: bytes, size: 32).get
stdout.write_bytes(bytes).get
}
}
```
Expand All @@ -124,9 +124,9 @@ import std.net.socket.TcpClient
class async Main {
fn async main {
let client = TcpClient.new(IpAddress.v4(0, 0, 0, 0), port: 9999).unwrap
let client = TcpClient.new(IpAddress.v4(0, 0, 0, 0), port: 9999).get
client.write_string('Hello, world!').unwrap
client.write_string('Hello, world!').get
}
}
```
Expand Down
4 changes: 3 additions & 1 deletion inko/src/command/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ fn generate_main_test_module(tests: Vec<ModuleName>) -> String {
source.push_str(&line);
}

source.push_str(" tests.filter = Filter.from_string(env.arguments.opt(0).unwrap_or(''))\n");
source.push_str(
" tests.filter = Filter.from_string(env.arguments.opt(0).or(''))\n",
);
source.push_str(" tests.run\n");
source.push_str(" }\n");
source.push_str("}\n");
Expand Down
10 changes: 5 additions & 5 deletions std/src/std/fs/file.inko
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class pub ReadOnlyFile {
#
# import std.fs.file.ReadOnlyFile
#
# let handle = ReadOnlyFile.new('/dev/null').unwrap
# let handle = ReadOnlyFile.new('/dev/null').get
fn pub static new[T: ToPath](path: ref T) -> Result[ReadOnlyFile, Error] {
let path = path.to_path

Expand Down Expand Up @@ -157,7 +157,7 @@ class pub WriteOnlyFile {
#
# import std.fs.file.WriteOnlyFile
#
# let file = WriteOnlyFile.new('/dev/null').unwrap
# let file = WriteOnlyFile.new('/dev/null').get
fn pub static new[T: ToPath](path: ref T) -> Result[WriteOnlyFile, Error] {
let path = path.to_path

Expand All @@ -177,7 +177,7 @@ class pub WriteOnlyFile {
#
# import std.fs.file.WriteOnlyFile
#
# let file = WriteOnlyFile.append('/dev/null').unwrap
# let file = WriteOnlyFile.append('/dev/null').get
fn pub static append[T: ToPath](path: ref T) -> Result[WriteOnlyFile, Error] {
let path = path.to_path

Expand Down Expand Up @@ -246,7 +246,7 @@ class pub ReadWriteFile {
#
# import std.fs.file.ReadWriteFile
#
# let handle = ReadWriteFile.new('/dev/null').unwrap
# let handle = ReadWriteFile.new('/dev/null').get
fn pub static new[T: ToPath](path: ref T) -> Result[ReadWriteFile, Error] {
let path = path.to_path

Expand All @@ -266,7 +266,7 @@ class pub ReadWriteFile {
#
# import std.fs.file.ReadWriteFile
#
# let handle = ReadWriteFile.append('/dev/null').unwrap
# let handle = ReadWriteFile.append('/dev/null').get
fn pub static append[T: ToPath](path: ref T) -> Result[ReadWriteFile, Error] {
let path = path.to_path

Expand Down
Loading

0 comments on commit 0536135

Please sign in to comment.