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
- Canonical layout: two spaces, colon-delimited indentation blocks. Tabs and ambiguous dedents are errors. Keep legacy braces only for migration. A future formatter must preserve comments and source maps; a parser/formatter round-trip test is a release gate.
- Immutable bindings and parameters by default;
mutopts into local mutation. Shadowing requires an explicit declaration in the stable design to avoid silently turning a misspelling into a new variable. The preview currently infers declaration on first assignment. - Explicit parameter/output/field contracts; local type inference.
T?is Optional<T>,T!is Result<T, Error>.T?!means Result<Optional<T>, Error>; other suffix order is rejected.!on a return type describes failure, NOT suspension. - No async/await keywords in user code. Compiler tracks an inferred suspension effect separately from the value/error type. Exported API metadata must record effects. Tools show suspension points; implicit async must not hide concurrency or cancellation.
- Only runtime-managed I/O may suspend. CPU code does not preempt automatically. CPU-bound tasks need explicit scheduling/offload APIs and budgets. Native blocking calls must be isolated and labelled; a promise of “no background threads anywhere” would be false for many TLS/DNS/database implementations.
spawncreates a child task owned by the current task group/request. No implicit detached tasks. Parent exit cancels and joins children before reclaiming their region.joinhas explicit all-results/fail-fast semantics; not yet chosen for the stable API.- Request arenas own request-local strings/records. A task cannot outlive its region. Cross-core messages transfer owned data; a durable copy/promotion is required for long-lived state. Buffer reuse waits for I/O cancellation/completion, including zero-copy notifications.
- Errors carry stable codes and an optional cause. Safe public text is distinct from sensitive diagnostic context. No secret URLs, passwords, SQL parameters or personal data in default logs.
- Protocol/crypto libraries are reused and maintained. ThreadOne will not invent a TLS stack or silently reimplement database wire protocols.
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.