Error codes · Types

PW0620

answer_read_for_a_value

The program must hold to this: what a command answers carries no value: a handler matches Ok(_), and reads the value from a query.

A program the compiler refuses with it

Refused: PW0620
module answer

import context.{ current_session }
import capability.{ Session, SessionId }

opaque type InteractionId = String

type Post = Post { id: String, text: String }

type PostError =
    | Unavailable

fn publish(s: Session<SessionId>, text: String) -> Result<Post, PostError> !{ database.write<Post> }
    host "blog:data/posts#publish"

command post(text: String) -> Result<Post, PostError>
    idempotent_by InteractionId
{
    publish(current_session(), text)
}

page Write() {
    route "/write"

    signal last: String = ""

    view {
        <title>Write</title>
        <button type="button" on:press={() => match post("Hello") {
            Ok(p) => last = "sent",
            Err(_) => (),
        }}>Post</button>
    }
}
pw check
error: [PW0620] `post` answers whether it committed, not its value: `p` is bound to nothing
  --> answer.pw:30:16
   |
30 |             Ok(p) => last = "sent",
   |                ^ `post` answers whether it committed, not its value: `p` is bound to nothing
   |
   = note: A command's caller is answered `Ok`, or an error the command declares (ADR-0157). The value it produced reaches the page from the query it invalidates, so the page has one source for what it shows: a value read from the answer would be a second, which the query's next change would contradict.
   = help: write `Ok(_)`, and read the value from the page's query

From Reading what a command returned, in Errors and diagnostics. A test holds the guide's examples to the compiler.

Reading the diagnostic

pw check prints the code in brackets, [PW0620], with the sentence above and the place in your program. Errors and diagnostics explains how to read one, and every code is listed with its rule.