ThreadOne language direction v0.1

Status: design draft, separate from SDK 0.6.1's implemented subset. The goal is code that is easy to inspect, not merely short to type. This draft does not claim Rust-level safety or C-level performance has been proven.

Decisions

Parsing or without two competing productions

One lowest-precedence, right-associative or production is sufficient. Its meaning is type-directed: Bool/Bool is short-circuit OR; Result<T,E>/T is lazy recovery; Optional<T>/T is lazy absence recovery. A Boolean Result needs explicit handling before combining logical operands. No implicit coercion from errors to false. or return, or fail and or err: are control-flow recovery forms and require a fallible/optional left operand.

In the final grammar, pipe binds above or and below logical and. A pipe target must be a named/member call, not an arbitrary expression. Left value becomes the first argument exactly once. A pipeline with multiple fallible stages short-circuits before later stages; the compiler must lower this explicitly, preserving error provenance. Preview SDK uses explicit recovery between fallible stages; automatic lifting is still pending.

program       = { type_decl | enum_decl | const_decl | fn_decl | route_decl } ;
fn_decl       = "fn" IDENT "(" [ params ] ")" [ "->" type ] ":" block ;
params        = param { "," param } ;
param         = IDENT ":" type ;
type          = IDENT [ "?" ] [ "!" ] ;
block         = NEWLINE INDENT statement { statement } DEDENT ;
statement     = binding | assignment | if_stmt | while_stmt | for_stmt
              | match_stmt | return_stmt | fail_stmt | expr_stmt ;
binding       = [ "mut" ] IDENT [ ":" type ] "=" expr NEWLINE ;
return_stmt   = "return" [ expr ] NEWLINE ;
fail_stmt     = "fail" expr NEWLINE ;
expr          = pipe_expr [ "or" recovery ] ;
recovery      = expr | "return" [ expr ] | "fail" expr | IDENT ":" block ;
pipe_expr     = and_expr { "|>" call_target } ;
call_target   = qualified_name [ "(" [ arguments ] ")" ] ;
and_expr      = compare_expr { "and" compare_expr } ;
compare_expr  = add_expr [ compare_op add_expr ] ;
add_expr      = mul_expr { ( "+" | "-" ) mul_expr } ;
mul_expr      = unary_expr { ( "*" | "/" | "%" ) unary_expr } ;
unary_expr    = ( "not" | "-" ) unary_expr | postfix_expr ;

This is a parser outline, not a complete grammar: pattern grammar, generic types, member access, literals, const evaluation and HTTP response construction still require normative productions. The current compiler implements return 201 result when the enclosing function returns Response, including routes; ordinary value-returning functions return one expression. Range 0..10 will be half-open, match exhaustive, and const values evaluated at compile time with defined overflow rules.

API example: partially implemented direction

type User:
  name: String
  email: String

route POST "/users" (req):
  user = req.json(User) or return 400 "Invalid JSON"
  result = users.create_with_outbox(user) or return 500 "Cannot create user"
  return 201 result |> to_public_view

The record, route, typed JSON and status-return syntax above runs in the current compiler, but users.create_with_outbox, modules and to_public_view are application/design placeholders. The complete example is not executable yet. For a working echo endpoint, see sdk/examples/users.one.

create_with_outbox must commit the user and durable email job in one transaction. A worker sends mail with an idempotency key and bounded retries. Sending mail directly after insert and returning 500 on mail failure risks duplicate user creation. The route also needs authentication/authorization, validation, uniqueness enforcement, idempotency and explicit error-to-status mapping. A database is not a transaction with an external mail service.

Implemented Optional subset

The preview accepts Optional Int, Bool, String and nonrecursive record values. Plain values require explicit some; none needs an expected Optional type. JSON missing/null Optional fields decode to absence, while required missing/null fields fail. Constructors still require all named fields. Optional records cannot be dereferenced until absence is handled, and equality does not narrow the type. Nested ??, Request?/Response?/Void?, and automatic pipeline lifting remain outside the implemented subset. Version 0.6.1 uses this model in redis_find(key) -> String?!, separating an absent key from command/decoding failure without changing redis_get. Consult sdk/SPEC.md for authoritative current behavior rather than treating the broader grammar above as complete.

Not yet implemented

Nested Optional types, enums, collections, generics, for/match, compile-time const, general methods/modules, bound error blocks, spawn/join, package management and language bindings for databases other than the experimental Redis basic-key API. Version 0.6 implements single-layer scalar/record Optionals (T?), explicit some/none, lazy absence recovery and T?! function results alongside immutable records, strict typed JSON, exact routes and implicit suspension on the current-thread executor; it does not complete the full design. Do not present this draft as the current SDK's accepted language.

Version 0.6.4 adds successful or return value in fallible functions, with the enclosing success type checked and lazy, once-only recovery. See the SDK specification.