-
Notifications
You must be signed in to change notification settings - Fork 0
feat(compiler): improve constraints, guards, and codegen #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
068399b
93285ce
1b2a38b
e192c72
d7f94a9
fd36f63
1383c4b
d4dc45c
b17e05b
7fbbdfa
0b3bdcd
e7591b2
8dfcf21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ import ShapesConstraintChaining from "/snippets/shapes-constraint-chaining.mdx"; | |
| import ShapesPlaceOrderTags from "/snippets/shapes-place-order-tags.mdx"; | ||
| import ShapesAddressCustomer from "/snippets/shapes-address-customer.mdx"; | ||
|
|
||
| In Go you declare a struct and write manual checks. In Forst, **constraints live on the type**. The compiler emits runtime validation at the boundary. | ||
| In Go you declare a struct and write manual checks. In Forst, **constraints live on the type**. Where values are known at compile time, the typechecker proves them statically; for dynamic input, use **`ensure … is …`** in the function body today. Automatic validation prologues on parameters are planned—not emitted yet. | ||
|
|
||
| ## Manual validation in Go | ||
|
|
||
|
|
@@ -35,7 +35,7 @@ The struct says nothing about valid ranges. Every handler repeats the same `if` | |
|
|
||
| <CatalogOrder /> | ||
|
|
||
| When `placeOrder` runs, the compiler has already proven the shape at compile time where values are known, and emits checks for dynamic input. Invalid SKUs and quantities never reach your catalog logic. | ||
| When `placeOrder` runs, the compiler has already proven the shape at compile time where values are known; use `ensure` (or explicit checks) for dynamic input. Invalid SKUs and quantities never reach your catalog logic. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win This still overstates the runtime guarantee. The surrounding edits correctly say dynamic validation happens via explicit 🧰 Tools🪛 LanguageTool[style] ~38-~38: It might be better to use ‘time’ with the time-relative pronoun ‘when’. (Alternatively, use ‘in/on which’.) (WHEN_WHERE) 🤖 Prompt for AI Agents |
||
|
|
||
| ## Built-in constraints | ||
|
|
||
|
|
@@ -95,7 +95,7 @@ Invalid nested data fails at the same boundary. You do not re-walk the tree in a | |
|
|
||
| ## What gets emitted | ||
|
|
||
| The compiler lowers constraints to plain Go checks before your function body runs: `String.Min(1)` becomes a `len(field)` comparison, `Int.Min(1)` a numeric comparison, and `HasPrefix` / `Contains` call `strings.*` with an auto-added import. Generated Go stays readable and uses standard error returns. There is no hidden magic at runtime. | ||
| The compiler lowers **`ensure`** checks in the function body to plain Go: `String.Min(1)` becomes a `len(field)` comparison, `Int.Min(1)` a numeric comparison, and `HasPrefix` / `Contains` call `strings.*` with an auto-added import. Parameter prologue validation (automatic checks before your function body) is not emitted yet—use `ensure` explicitly today. | ||
|
|
||
| ## Try it | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,10 @@ | |
| </Tab> | ||
| <Tab title="Generated Go"> | ||
| ```go | ||
| // forst build | ||
| if x, ok := x.(Ok); ok { | ||
| println(x.Value) | ||
| // forst build — Result lowers to (T, error) | ||
| x, xErr := one() | ||
| if xErr == nil { | ||
| println(x) | ||
|
Comment on lines
+11
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Keep the Generated Go tab aligned with the Forst tab. The Forst example starts from an existing 🤖 Prompt for AI Agents |
||
| } | ||
| ``` | ||
| </Tab> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ | |
| </Tab> | ||
| <Tab title="Generated Go"> | ||
| ```go | ||
| // forst build | ||
| // forst build — illustrative; type-level guards do not emit Go helpers yet | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win This comment contradicts the feature added in this PR. The stack context for this change says type-level guards now emit stub Go declarations, so saying they “do not emit Go helpers yet” is immediately stale. Please update the note and snippet to describe the stub output instead of framing it as purely illustrative. 🤖 Prompt for AI Agents |
||
| func G_Input(m MutationArg, input Shape) error { | ||
| // shape guard: m must include input field | ||
| return nil | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Escape the pipe in this table cell.
Line 78 is currently parsed as a 4-column row, so the binary-types entry will not render reliably. Use a representation that avoids a raw
|inside the table cell.🧰 Tools
🪛 markdownlint-cli2 (0.22.1)
[warning] 78-78: Spaces inside code span elements
(MD038, no-space-in-code)
[warning] 78-78: Spaces inside code span elements
(MD038, no-space-in-code)
[warning] 78-78: Spaces inside code span elements
(MD038, no-space-in-code)
[warning] 78-78: Table column count
Expected: 3; Actual: 4; Too many cells, extra data will be missing
(MD056, table-column-count)
🤖 Prompt for AI Agents
Source: Linters/SAST tools