Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions docs/option.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,30 @@ distinct from the VM's internal Null value and from the legacy nullable
## Option values

```rust
let some: Option<int> = Option::Some(0);
let none: Option<int> = Option::None;
let some: Option<int> = Some(0);
let none: Option<int> = None;

let value = match some {
Option::Some(inner) => inner,
Option::None => -1,
Some(inner) => inner,
None => -1,
};
```

`Option::Some` may infer its payload type when the payload itself has a fully
known schema. `Option::None` has no payload from which to infer `T`, so it
`Some` may infer its payload type when the payload itself has a fully
known schema. `None` has no payload from which to infer `T`, so it
requires an `Option<T>` annotation or another contextual type. Missing type
arguments are rejected instead of being invented as `Unknown`.

A match on a logical Option must provide `Option::Some` and `Option::None`; the
two arms are exhaustive. A wildcard arm may stand in for `Option::None` when the
`Some` and `None` are reserved constructor names in RustScript expressions and
patterns; they cannot be used as variable, parameter, or function names. Member
access such as `obj.None` and a quoted map key such as `{"None": 1}` remain
valid. The former `Option::Some` and `Option::None` spellings have been removed.
In a match, `Some(TypeName)` now binds the Option payload to `TypeName`; it no
longer performs a runtime type test. Other parser dialects retain their own
legacy optional-slot patterns.

A match on a logical Option must provide `Some` and `None`; the
two arms are exhaustive. A wildcard arm may stand in for `None` when the
Some arm is present. Duplicate arms, mixing Result construction arms into the
same match, and ambiguous wildcard combinations are rejected.

Expand Down Expand Up @@ -124,12 +132,12 @@ the current no-std resource capability and is refused before execution.
`unwrap_or_else`, `ok_or` and `ok_or_else`.

```rust
let some: Option<int> = Option::Some(2);
let absent: Option<int> = Option::None;
let some: Option<int> = Some(2);
let absent: Option<int> = None;

let mapped = some.map(|value| value + 1); // Option<int>, wraps U
let flat = some.and_then(|value| Option::Some(value + 1)); // flattens one layer
let kept = absent.or_else(|| Option::Some(3)); // zero-argument lazy fallback
let flat = some.and_then(|value| Some(value + 1)); // flattens one layer
let kept = absent.or_else(|| Some(3)); // zero-argument lazy fallback
let eager = some.unwrap_or(0); // eager fallback, returns int
let lazy = absent.unwrap_or_else(|| 4); // lazy body, returns int
let as_result = absent.ok_or("missing"); // Result<int, string>
Expand Down Expand Up @@ -169,9 +177,9 @@ declared cleanup. A None inside a closure does not return from its caller.

```rust
fn relay(flag: bool) -> Option<int> {
let value: Option<int> = if flag => { Option::Some(1) } else => { Option::None };
let value: Option<int> = if flag => { Some(1) } else => { None };
let item = value?;
Option::Some(item + 1)
Some(item + 1)
}
```

Expand All @@ -191,10 +199,10 @@ A non-optional receiver performs one fallible projection. An Option receiver
consumes exactly one and_then-equivalent receiver layer; the projected payload
keeps its own schema, so a declared `Option<int>` leaf produces
`Option<Option<int>>` rather than an arbitrary flattening. An absent dynamic key
and a present key holding explicit `Option::None` remain distinct.
and a present key holding explicit `None` remain distinct.

Permitted omitted logical Option fields normalize to None at the RustScript
boundary, while required fields are still checked. An explicit Option::None
boundary, while required fields are still checked. An explicit None
field is a tagged value, not a map deletion. The internal VM keeps its raw Null
value and other frontends keep their legacy nullable contracts; RustScript
itself rejects explicit null literals and legacy `T?` annotations.
Expand Down
2 changes: 1 addition & 1 deletion docs/result.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ becomes `None`.
let value: Result<int, string> = Result::Ok(21);
let ok_value = value.ok();
let err_value = value.err();
let selected = match ok_value { Option::Some(inner) => inner, Option::None => -1 };
let selected = match ok_value { Some(inner) => inner, None => -1 };
selected;
```

Expand Down
66 changes: 33 additions & 33 deletions docs/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ Execution also requires an async host bridge and the SQLite module registered.
```rust
use sqlite;

let limits: SqliteLimits = { max_rows: Option::Some(100) };
let limits: SqliteLimits = { max_rows: Some(100) };
let options: SqliteOpenOptions = {
path: Option::Some(":memory:"),
mode: Option::Some("memory"),
limits: Option::Some(limits),
path: Some(":memory:"),
mode: Some("memory"),
limits: Some(limits),
};
let db = sqlite::open(options);
```

Every field is a logical `Option`. `Option::None` keeps the host default; `Option::Some(value)`
Every field is a logical `Option`. `None` keeps the host default; `Some(value)`
explicitly supplies it, including `Some(false)`, `Some(0)`, and `Some("")`. A `path` of
`Option::None` is still rejected at runtime for a usable connection.
`None` is still rejected at runtime for a usable connection.

| Field | Type | Runtime behavior |
| --- | --- | --- |
Expand All @@ -53,7 +53,7 @@ Unknown fields are outside the named-struct contract; do not rely on them.

## Limits (`SqliteLimits`)

Every limit is `Option<int>`. `Option::None` keeps the embedding ceiling; `Option::Some(0)` is a
Every limit is `Option<int>`. `None` keeps the embedding ceiling; `Some(0)` is a
provided invalid value rather than a default. Supplied limits must be positive and cannot exceed
that ceiling.

Expand Down Expand Up @@ -89,21 +89,21 @@ The five `kind` values and their selected payloads are:

| `kind` | Required payload | Other payloads |
| --- | --- | --- |
| `"null"` | none | all payloads `Option::None` |
| `"int"` | `int_value` | other payloads `Option::None` |
| `"float"` | `float_value` | other payloads `Option::None` |
| `"text"` | `text_value` | other payloads `Option::None` |
| `"blob"` | `blob_value` | other payloads `Option::None` |
| `"null"` | none | all payloads `None` |
| `"int"` | `int_value` | other payloads `None` |
| `"float"` | `float_value` | other payloads `None` |
| `"text"` | `text_value` | other payloads `None` |
| `"blob"` | `blob_value` | other payloads `None` |

For example:

```rust
let values = [
{ kind: "null" },
{ kind: "int", int_value: Option::Some(7) },
{ kind: "float", float_value: Option::Some(1.5) },
{ kind: "text", text_value: Option::Some("hello") },
{ kind: "blob", blob_value: Option::Some(bytes::from_hex("000102")) },
{ kind: "int", int_value: Some(7) },
{ kind: "float", float_value: Some(1.5) },
{ kind: "text", text_value: Some("hello") },
{ kind: "blob", blob_value: Some(bytes::from_hex("000102")) },
];
```

Expand All @@ -116,14 +116,14 @@ with its original bytes.

SQLite exposes three distinct absence forms and they are not interchangeable:

- Configuration and statement entries use logical `Option`: `Option::None` keeps the
documented default and `Option::Some(value)` supplies it. A physical `null` scalar is
- Configuration and statement entries use logical `Option`: `None` keeps the
documented default and `Some(value)` supplies it. A physical `null` scalar is
not a second reading of a present public field.
- A SQL `NULL` result cell remains the tagged `SqliteValue { kind: "null", ... }`
business value; its four payload slots are logical `Option::None`. It is not an
business value; its four payload slots are logical `None`. It is not an
absent row and it is not rewritten into an option-of-cell.
- Result-value absence uses logical `Option`: `SqliteQueryResult.next_cursor` and
the unselected `SqliteTransactionResult` branch are `Option::Some`/`Option::None`
the unselected `SqliteTransactionResult` branch are `Some`/`None`
values, matched with `match` rather than a sentinel.

## Execute (`SqliteExecuteResult`)
Expand All @@ -134,7 +134,7 @@ SQLite exposes three distinct absence forms and they are not interchangeable:
let inserted = sqlite::execute(
&db,
"INSERT INTO t (value) VALUES (?1)",
[{ kind: "int", int_value: Option::Some(7) }],
[{ kind: "int", int_value: Some(7) }],
);
let affected = inserted.rows_affected;
let rowid = inserted.last_insert_rowid;
Expand All @@ -153,7 +153,7 @@ limits before the adapter call is awaited.
`sqlite::query` takes an `array<SqliteValue>` and returns a `SqliteQueryResult`:

```rust
let limits: SqliteLimits = { max_rows: Option::Some(32) };
let limits: SqliteLimits = { max_rows: Some(32) };
let queried = sqlite::query(&db, "SELECT value FROM t ORDER BY rowid", [], limits);

let first_row = queried.rows[0];
Expand All @@ -174,13 +174,13 @@ Each `SqliteRow` has one field, `cells`, an array of `SqliteValue` in column ord
limits are charged from the underlying column names and cell payloads, without counting the
named-struct wrapper fields. `max_rows`, `max_columns`, and `max_result_bytes` can truncate a
query; already accepted rows remain in the result. `next_cursor` is
`Option::Some(first-column integer)` from the last accepted row, or `Option::None`
`Some(first-column integer)` from the last accepted row, or `None`
when no such value was accepted:

```rust
let next = match queried.next_cursor {
Option::Some(cursor) => cursor,
Option::None => -1,
Some(cursor) => cursor,
None => -1,
};
```

Expand All @@ -193,12 +193,12 @@ A transaction receives an array of named `SqliteStatement` values. Each statemen
let results = sqlite::transaction(&db, [
{
sql: "INSERT INTO t (value) VALUES (?1)",
params: Option::Some([{ kind: "int", int_value: Option::Some(8) }]),
params: Some([{ kind: "int", int_value: Some(8) }]),
},
{
sql: "SELECT value FROM t ORDER BY rowid",
query: Option::Some(true),
limits: Option::Some(limits),
query: Some(true),
limits: Some(limits),
},
]);
```
Expand All @@ -220,15 +220,15 @@ SqliteTransactionResult {
}
```

An execute statement produces `{ kind: "execute", execute: Option::Some(...) }`; a query statement
produces `{ kind: "query", query: Option::Some(...) }`. The unselected envelope field is
`Option::None`. Discriminate with `kind` before matching `execute` or `query`:
An execute statement produces `{ kind: "execute", execute: Some(...) }`; a query statement
produces `{ kind: "query", query: Some(...) }`. The unselected envelope field is
`None`. Discriminate with `kind` before matching `execute` or `query`:

```rust
let result = results[0];
let executed = match result.execute {
Option::Some(value) => value.rows_affected,
Option::None => -1,
Some(value) => value.rows_affected,
None => -1,
};
``` The transaction remains atomic: statement order,
rollback on failure, transaction deadlines, and result limits are preserved. A SQLite progress
Expand Down
4 changes: 2 additions & 2 deletions examples/example_complex.rss
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ let closure_value = add(5);

let profile: Profile = {stats: {score: closure_value}};
let matched = match profile?.stats?.score {
Option::None => 0,
Option::Some(score) => if score == 12 => {
None => 0,
Some(score) => if score == 12 => {
closure_value
} else => {
0
Expand Down
20 changes: 10 additions & 10 deletions examples/sqlite_named_structs.rss
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use sqlite;
use bytes;

let limits: SqliteLimits = { max_rows: Option::Some(32) };
let limits: SqliteLimits = { max_rows: Some(32) };
let options: SqliteOpenOptions = {
path: Option::Some(":memory:"),
mode: Option::Some("memory"),
limits: Option::Some(limits)
path: Some(":memory:"),
mode: Some("memory"),
limits: Some(limits)
};
let db = sqlite::open(options);

let empty: [SqliteValue] = [];
sqlite::execute(&db, "CREATE TABLE t (value, payload)", empty);
let blob_payload = bytes::from_hex("000102");
let inserted = sqlite::execute(&db, "INSERT INTO t VALUES (?1, ?2)", [
{ kind: "int", int_value: Option::Some(7) },
{ kind: "blob", blob_value: Option::Some(blob_payload) }
{ kind: "int", int_value: Some(7) },
{ kind: "blob", blob_value: Some(blob_payload) }
]);
let affected = inserted.rows_affected;
assert(affected == 1);
Expand All @@ -27,18 +27,18 @@ let first_value = first.int_value.unwrap_or(-1);
assert(first_value == 7);

let insert_params: [SqliteValue] = [
{ kind: "text", text_value: Option::Some("hello") },
{ kind: "text", text_value: Some("hello") },
{ kind: "null" }
];
let statements: [SqliteStatement] = [
{
sql: "INSERT INTO t VALUES (?1, ?2)",
params: Option::Some(insert_params)
params: Some(insert_params)
},
{
sql: "SELECT value, payload FROM t ORDER BY rowid",
query: Option::Some(true),
limits: Option::Some(limits)
query: Some(true),
limits: Some(limits)
}
];
let results = sqlite::transaction(&db, statements);
Expand Down
20 changes: 10 additions & 10 deletions pd-vm-nostd/tests/call_script_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn nostd_executes_std_encoded_result_ok_and_err_without_source() {
EmbeddedValue::Bool(false),
),
(
"let value: Result<string, Option<int>> = Result::Err(Option::None); match value { Result::Ok(s) => Option::Some(9), Result::Err(n) => n, };",
"let value: Result<string, Option<int>> = Result::Err(None); match value { Result::Ok(s) => Some(9), Result::Err(n) => n, };",
EmbeddedValue::map(vec![(
EmbeddedValue::string("tag"),
EmbeddedValue::Bool(false),
Expand Down Expand Up @@ -167,36 +167,36 @@ fn nostd_executes_std_encoded_result_ok_and_err_without_source() {
fn nostd_executes_std_encoded_option_and_unit_without_source() {
let cases = [
(
"let nested: Option<Option<int>> = Option::Some(Option::None); match nested { Option::Some(inner) => match inner { Option::Some(v) => v, Option::None => 0 }, Option::None => -1 };",
"let nested: Option<Option<int>> = Some(None); match nested { Some(inner) => match inner { Some(v) => v, None => 0 }, None => -1 };",
EmbeddedValue::Int(0),
),
(
"let value: Option<Result<int, string>> = Option::Some(Result::Ok(1)); match value { Option::Some(result) => match result { Result::Ok(v) => v, Result::Err(e) => -1 }, Option::None => -2 };",
"let value: Option<Result<int, string>> = Some(Result::Ok(1)); match value { Some(result) => match result { Result::Ok(v) => v, Result::Err(e) => -1 }, None => -2 };",
EmbeddedValue::Int(1),
),
(
"let value: Result<Option<int>, string> = Result::Ok(Option::Some(2)); match value { Result::Ok(option) => match option { Option::Some(v) => v, Option::None => -4 }, Result::Err(e) => -8 };",
"let value: Result<Option<int>, string> = Result::Ok(Some(2)); match value { Result::Ok(option) => match option { Some(v) => v, None => -4 }, Result::Err(e) => -8 };",
EmbeddedValue::Int(2),
),
(
"let value: Option<()> = Option::Some(()); match value { Option::Some(unit) => 7, Option::None => -1 };",
"let value: Option<()> = Some(()); match value { Some(unit) => 7, None => -1 };",
EmbeddedValue::Int(7),
),
(
"let value: Option<bool> = Option::Some(false); match value { Option::Some(flag) => flag, Option::None => true };",
"let value: Option<bool> = Some(false); match value { Some(flag) => flag, None => true };",
EmbeddedValue::Bool(false),
),
(
"let value: Option<int> = Option::Some(0); match value { Option::Some(v) => v, Option::None => 1 };",
"let value: Option<int> = Some(0); match value { Some(v) => v, None => 1 };",
EmbeddedValue::Int(0),
),
(
"let value: Option<string> = Option::Some(\"\"); match value { Option::Some(text) => text, Option::None => \"missing\" };",
"let value: Option<string> = Some(\"\"); match value { Some(text) => text, None => \"missing\" };",
EmbeddedValue::string(""),
),
("let unit: () = (); unit;", EmbeddedValue::Null),
(
"let value: Option<int> = Option::None; match value { Option::Some(v) => v, Option::None => 9 };",
"let value: Option<int> = None; match value { Some(v) => v, None => 9 };",
EmbeddedValue::Int(9),
),
];
Expand Down Expand Up @@ -620,7 +620,7 @@ fn nostd_reads_option_cleanup_schema_and_refuses_the_resource_capability() {
let catalog = Arc::new(builder.build().expect("catalog"));
let source = r#"
fn relay() -> Result<int, string> {
let local: Option<resource<pr24.closure>> = Option::Some(test::make_local());
let local: Option<resource<pr24.closure>> = Some(test::make_local());
let value = test::fail()?;
Result::Ok(value)
}
Expand Down
2 changes: 1 addition & 1 deletion pd-vm-nostd/tests/embedded_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn std_encoded_json_programs_fail_nostd_at_intended_ids() {
vm::BuiltinFunction::JsonEncodeTyped.call_index(),
),
(
"use json; struct Payload { value: Option<()> } let payload: Payload = {value: Option::Some(())}; json::encode(payload);",
"use json; struct Payload { value: Option<()> } let payload: Payload = {value: Some(())}; json::encode(payload);",
vm::BuiltinFunction::JsonEncodeTyped.call_index(),
),
];
Expand Down
8 changes: 4 additions & 4 deletions pd-vm-wasm/examples/rss-collections-iter-example.rss
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ let reversed_preview: [string] = collections::reverse::<string>(["world", "finan
let combined_names: [string] = reversed_preview + ["archive"];
let archive_index: Option<int> = collections::index_of::<string>(&combined_names, "archive");
let archive_index_matches: bool = match archive_index {
Option::Some(value) => value == 2,
Option::None => false,
Some(value) => value == 2,
None => false,
};
assert(archive_index_matches);
let missing_index: bool = match collections::index_of::<string>(&combined_names, "sports") {
Option::Some(_) => false,
Option::None => true,
Some(_) => false,
None => true,
};
assert(missing_index);
assert(collections::contains::<string>(&combined_names, "world"));
Expand Down
Loading
Loading