Skip to content

feat: test a value's type with x as? T 🔍 - #347

Merged
timfennis merged 2 commits into
feat/type-checkerfrom
feature/optional-cast
Nov 13, 2026
Merged

timfennis merged 2 commits into
feat/type-checkerfrom
feature/optional-cast

Conversation

@timfennis

Copy link
Copy Markdown
Owner

Making Any explicit (#331) leaves a cast as the only way to get a value out of Any, and there was no way to test a value's type: x as T stops the program when x isn't a T. x as? T is that test: Some(x), as a T, when the value is a T, and None when it isn't. It has type Option<T>.

let v: Any = 3;
if (v as? Int).is_some() { print("an int"); }
print([1, "a"] as? List<Int>);   // None
  • Lexer, parser, formatter, highlighter and LSP completion know as?, which has the precedence of as (so (x as? Int).is_some() needs its parentheses) and needs a space after it, as ? can be part of a name.
  • The analyser checks it as it checks as: a cast that can never succeed is an error (it would always be None), and one it can prove needs no check. Unlike as, it doesn't settle what isn't known about the value: testing xs as? List<Int> leaves an empty list free to hold Strings. A number written there still takes the type: 1 as? Float is Some(1.0).
  • A new TryCast instruction converts the value as as does, functions included (they get their calls checked), and gives None instead of failing. Its body is out of the VM's main loop, so calls run exactly as before (fib(27) executes the same number of instructions).

The manual's casts page has a section on it.

🤖 Generated with Claude Code

timfennis and others added 2 commits November 13, 2026 22:11
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T21:14:47.774011Z f6111db PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@timfennis
timfennis merged commit 1f70dd0 into feat/type-checker Nov 13, 2026
1 check passed
@timfennis
timfennis deleted the feature/optional-cast branch November 13, 2026 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant