ASPL لباتASPL 0.7.0

Search documentation

Search starts with current English documentation · ASPL 0.7.0

Enter a complete name such as read_line, or words about a topic.

    العربية
    Source files

    Open one UTF-8 .aspl or .لبات file, up to 1 MiB, in a new draft. It stays in this browser and does not run automatically.

    Recoverable results

    ASPL 0.7.0 · Current release

    Return a greeting or an empty-name failure and read only the matching payload.

    On this page

    What you will learn

    Return a successful greeting for a nonempty name or an empty-name failure for empty text. Handle both outcomes using only the matching payload, and distinguish results from traps and source errors.

    Before you begin

    Read functions, conditions and numeric parsing for the earlier result template. This independent program includes its helper and entry. Open a new Home draft and choose Run explicitly; earlier drafts remain available.

    Return a value or error information

    fn greet(name: string) -> result<string> / دالة حي(اسم: نص) -> نتيجة<نص> declares a helper whose successful payload is text. name matches "" / اسم يطابق "" compares string contents. An empty name returns failure("empty-name", "name is empty") / إخفاق(...). The return type supplies the expected result<string> type for failure, which has no success payload. Nonempty text returns success("Hello, " + name + "!") / نجاح(...). Both paths return a result.

    Browser and native · ASPL 0.7.0 · English source

    Execution: Browser and native · ASPL 0.7.0

    Prerequisites:
    • Read the example explanation in its lesson before running.

    Input: No input lines are required.

    fn greet(name: string) -> result<string> {
        if (name matches "") {
            return failure("empty-name", "name is empty");
        }
        return success("Hello, " + name + "!");
    }
    fn start_main() -> i64 {
        let greeting: result<string> = greet("سلمى");
        if (greeting::ok) {
            print(greeting::value + "\n");
        } else {
            print(greeting::error::code + "\n");
        }
        let empty: result<string> = greet("");
        if (empty::ok) {
            print(empty::value + "\n");
        } else {
            print(empty::error::code + "\n");
        }
        return 0;
    }
    
    Download lesson-results.aspl

    Expected outcomes
    {
      "browser": {
        "check": "success",
        "class": "success",
        "output": "Hello, سلمى!\nempty-name\n",
        "processStatus": 0
      },
      "native": {
        "check": 0,
        "status": 0,
        "output": "Hello, سلمى!\nempty-name\n"
      }
    }

    The caller invokes the helper first with "سلمى", then with "". ::ok / ::نجاح tests the Boolean branch. Only the success branch prints ::value / ::قيمة; only the failure branch reads ::error::code / ::الخطأ::الرمز. Each line adds an explicit \n. error_info / معلومات_خطأ contains a stable code / الرمز and a descriptive message / الرسالة. Use codes for decisions, not message wording. A helper chooses its own code and message.

    A failed result is ordinary data, emits no diagnostic and permits later execution. It is different from a source error, which prevents execution, and an uncatchable trap, which ends a run. The entry's return 0 / أرجع 0 is the normal process status after handling both results, not a greeting payload or an error code. Stop and resource limits are separate outcomes too.

    Predict both branches

    Before Run, identify which payload each call may read. Exact output is Hello, سلمى!\nempty-name\n, with status 0. The failed second call cannot supply text. A string containing spaces is nonempty; this helper does not trim it.

    This separately labelled intentional failure reads value from a failed result. Checking succeeds, but execution traps with wrong-result-branch, prints nothing and has no normal entry status. Reading error on success is wrong too. The Arabic source uses ::قيمة; the English source uses ::value. The published native ASPL 0.7.0 executable reports the same trap and exits with status 5, which is not a normal entry status. Guarded lesson sources use the correct branch on both hosts.

    Browser and native · ASPL 0.7.0 · English source

    Execution: Browser and native · ASPL 0.7.0

    Prerequisites:
    • Read the example explanation in its lesson before running.

    Input: No input lines are required.

    fn start_main() -> i64 {
        let reply: result<string> = failure("empty-name", "name is empty");
        print(reply::value);
        return 0;
    }
    
    Download lesson-result-wrong-payload.aspl

    Expected outcomes
    {
      "browser": {
        "check": "success",
        "class": "trap",
        "output": "",
        "reason": "wrong-result-branch"
      },
      "native": {
        "check": 0,
        "status": 5,
        "output": "",
        "reason": "wrong-result-branch"
      }
    }

    Change the successful greeting

    Change only the helper's successful prefix from "Hello, " to "Welcome, ". Keep its empty-name failure and both caller branches. Expected output is exactly Welcome, سلمى!\nempty-name\n, with normal status 0.

    Change the successful greeting to Welcome, and predict both calls.

    Hint: Edit the literal inside success. Leave the empty-name test, failure and ok guards unchanged.

    Show worked answer

    The nonempty name reaches success, so the caller's success branch prints Welcome, سلمى! and a newline. Empty text still returns empty-name with no successful payload. Only its failure branch reads the error code and prints it. Handling that failed result allows the entry to continue and return normal status 0.

    Browser and native · ASPL 0.7.0 · English source

    Execution: Browser and native · ASPL 0.7.0

    Prerequisites:
    • Read the example explanation in its lesson before running.

    Input: No input lines are required.

    fn greet(name: string) -> result<string> {
        if (name matches "") {
            return failure("empty-name", "name is empty");
        }
        return success("Welcome, " + name + "!");
    }
    fn start_main() -> i64 {
        let greeting: result<string> = greet("سلمى");
        if (greeting::ok) {
            print(greeting::value + "\n");
        } else {
            print(greeting::error::code + "\n");
        }
        let empty: result<string> = greet("");
        if (empty::ok) {
            print(empty::value + "\n");
        } else {
            print(empty::error::code + "\n");
        }
        return 0;
    }
    
    Download lesson-results-solution.aspl

    Expected outcomes
    {
      "browser": {
        "check": "success",
        "class": "success",
        "output": "Welcome, سلمى!\nempty-name\n",
        "processStatus": 0
      },
      "native": {
        "check": 0,
        "status": 0,
        "output": "Welcome, سلمى!\nempty-name\n"
      }
    }

    Check your understanding

    Explain where failure gets its payload type and why neither wrong payload is safe. Contrast empty-name, wrong-result-branch and a wrong return type. No grade or progress is stored, and navigation stays open. Return to the Tutorial path or read the results reference.

    Browse pages