Skip to content

feat: a PHP# template shows an int, a float, a string or a bool, and a bool as true or false - #83

Merged
heyJordanParker merged 6 commits into
masterfrom
feat/sharp-template-values
Oct 9, 2026
Merged

heyJordanParker merged 6 commits into
masterfrom
feat/sharp-template-values

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 9, 2026 •

Copy link
Copy Markdown
Owner

Problem

A PHP# developer writes `Paid: ${isPaid}` and expects "Paid: true". A template passed every value to PHP's string conversion, so a bool printed 1 or nothing. An enum, a collection or an object fell through to PHP's runtime error, "Cannot cast object of type Plain to string because it does not implement Stringable", which tells the developer to write __toString(), a method PHP# refuses.

Solution

Spec section 18 states the rule, the bool text and the exact message:

const a = `Total: ${count}`;   // int: "Total: 3"
const b = `Paid: ${isPaid}`;   // bool: "Paid: true"
const c = `Status: ${status}`; // enum case: compile error
const d = `Items: ${items}`;   // List<int>: compile error
const e = `Order: ${order}`;   // class Order: compile error

The error is "A template shows int, float, string or bool, and Status is none of them.", with the offending type's PHP# name. A literal or narrowed type counts as its base type. A union such as int|string is none of the four and is refused. int? is refused until it is checked, as section 24 already states. A plain PHP file keeps PHP's own interpolation.

Section 21's match example showed `Continue at ${redirect}` with redirect bound as a Url, which the new rule refuses. The spec declares no Url and gives it no text form. The standard library's Sharp.Net.Url is a static class of string helpers with no instances. So PaymentResult.RequiresAction now carries string redirect, in its declaration in section 20 and in the arm in section 21.

Decision 70 records it. It chooses int, float, string and bool with a bool printed by name, quoting Kotlin (true) and C# (True). It rejects only numbers and strings (Elm) and a default text for anything (Swift and JavaScript). Each option shows the same five lines.

SHARP_MAGO_COMMIT moves to a59162f, the merge commit of heyJordanParker/mago-sharp#110. That commit's checker gives the error. Its bridge lowers a bool part as ["false", "true"][value], one FETCH_DIM_R after the optimizer. The header the bridge generates at that commit differs from this one only in the commit line, and SHARP_UNIT_ABI is unchanged.

docs/
├── spec.md                          <- section 18: the rule, the bool text, the message, unions refused, five examples
│                                       sections 20 and 21: RequiresAction carries `string redirect`
└── decisions/070-template-values.md <- new: chosen, two rejected options, Kotlin and C# quoted, spec
ext/sharp/sharp_unit.h               <- SHARP_MAGO_COMMIT a59162f6e, the merge of mago-sharp#110
Zend/tests/sharp/
├── Template.sharp                   <- paid(bool) and totals(int, float, string)
└── template.phpt                    <- "Paid: true", "Paid: false", "Total: 3, ratio 1.5, name cart"; title states the rule

Verification

  • template.phpt on the old pin 58c7c4a prints Paid: 1 and Paid:, and fails.
  • The gate passes in all 22 modes, 161 of 161 tests each with 0 skipped. That covers plain, the opcache file cache priming and using it, repeat, the 16 single optimizer passes, and the tracing and function JITs.
  • The census prints "Checked 228 files against the PHP# marks on 35 opcodes and AST kinds.", and php -l compiles all 74 fixtures.
  • mago compile at 6a5b4f1 compiles all 96 .sharp files and refuses none, so no fixture or library file changed.
  • regen-sharp-kinds.php at 6a5b4f1 reproduces kind.rs with an empty diff.
  • The full gate ran at the PR head pin 6a5b4f1. After the re-pin to the merge commit a59162f, regen-sharp-kinds.php reproduces kind.rs with an empty diff. The generated header differs only in SHARP_MAGO_COMMIT. mago compile compiles all 96 .sharp files, and template.phpt, phpversion_sharp.phpt and ri_sharp.phpt pass in plain mode and under both JITs.
  • php --ri sharp prints Mago commit => a59162f6e8f08ff6b7358abee0ca3b71907e00e6.
  • No gate compiles the spec's examples. sharp/bin/test compiles only Zend/tests/sharp/*.sharp, the harness and the library, and nothing reads docs/spec.md.
  • The section 20/21 example as written cannot compile today: the pinned checker refuses its data-carrying cases and case patterns as "not supported yet in PHP#". So the changed template part was compiled alone at 6a5b4f1. `Continue at ${redirect}` with string redirect compiles. With an object it gets "A template shows int, float, string or bool, and Redirect is none of them."

…a bool as true or false

Problem

A PHP# developer writes `` `Paid: ${isPaid}` `` and expects "Paid: true".
A template passed every value to PHP's string conversion, so a `bool`
printed `1` or nothing. An enum, a collection or an object fell through to
PHP's runtime error, "Cannot cast object of type `Plain` to `string` because
it does not implement `Stringable`", which tells the developer to write
`__toString()`, a method PHP# refuses. The spec did not say which values a
template shows.

Solution

Spec section 18 states the rule, and decision 70 records it with Kotlin
and C# as precedent:

- A template's `${expr}` accepts `int`, `float`, `string` and `bool`, and a
  `bool` prints `true` or `false`.
- Every other type is a compile error: "A template shows `int`, `float`,
  `string` or `bool`, and `Status` is none of them.", naming the type.
- A nullable value is refused until it is checked, as section 24 states.
- A plain PHP file keeps PHP's own interpolation.

`SHARP_MAGO_COMMIT` moves to mago-sharp d87f79a, whose checker gives the
error and whose bridge lowers a `bool` part as `value ? "true" : "false"`.
The header the bridge generates at that commit differs from this one only
in that line.

docs/
├── spec.md                          <- section 18: the rule, the bool text, the message, five examples
└── decisions/070-template-values.md <- new: chosen, two rejected options, precedent, spec
ext/sharp/sharp_unit.h               <- SHARP_MAGO_COMMIT d87f79a
Zend/tests/sharp/
├── Template.sharp                   <- paid(bool): `Paid: ${isPaid}`
└── template.phpt                    <- prints "Paid: true" and "Paid: false"; title states the rule
…pin the one-instruction bool lowering

Problem

`template.phpt` showed a `bool` printing `true` and `false`, but no line
showed `int`, `float` and `string` interpolating as before in the same
template. Decision 70 linked Kotlin and C# without quoting them, and neither
it nor spec section 18 said that a union such as `int|string` is refused.
mago-sharp#110 now lowers a `bool` part in one instruction.

Solution

- `Template::totals(int, float, string)` prints "Total: 3, ratio 1.5, name
  cart", and `template.phpt` expects it.
- Spec section 18 and decision 70 state that `int|string` is refused.
- Decision 70 quotes Kotlin's and C#'s documentation and `Boolean.ToString`.
- `SHARP_MAGO_COMMIT` moves to mago-sharp 6a5b4f1, whose bridge lowers a
  `bool` part as `["false", "true"][value]`, one FETCH_DIM_R after the
  optimizer. The generated header differs only in that line, and
  `SHARP_UNIT_ABI` is unchanged.

docs/
├── spec.md                          <- section 18: a union such as int|string is refused
└── decisions/070-template-values.md <- refused unions; Kotlin and C# quoted
ext/sharp/sharp_unit.h               <- SHARP_MAGO_COMMIT 6a5b4f1
Zend/tests/sharp/
├── Template.sharp                   <- totals(int, float, string)
└── template.phpt                    <- expects "Total: 3, ratio 1.5, name cart"
…h a template shows

Problem

Spec section 21's `match` example shows `` `Continue at ${redirect}` ``
with `redirect` bound as a `Url`. Section 18 now refuses every template
part that is not an `int`, a `float`, a `string` or a `bool`, so the spec
showed code its own rule refuses. The spec declares no `Url` type and gives
it no text form. The standard library's `Sharp.Net.Url` is a static class
of string helpers with no instances, so it cannot hold a field's value.

Solution

`PaymentResult.RequiresAction` carries `string redirect`, in the enum's
declaration in section 20 and in the `match` arm in section 21. The arm
then shows a `string`, which section 18 accepts.

docs/
└── spec.md   <- section 20: case RequiresAction(string redirect);
                 section 21: PaymentResult.RequiresAction(string redirect) => `Continue at ${redirect}`,
Problem

`SHARP_MAGO_COMMIT` named 6a5b4f1, the head of mago-sharp#110 before it
merged. php-sharp pins only commits on mago-sharp's master.

Solution

`ext/sharp/sharp_unit.h` is the header the bridge generates at a59162f,
the merge commit of mago-sharp#110. It differs only in `SHARP_MAGO_COMMIT`.
`SHARP_UNIT_ABI` is unchanged, and `regen-sharp-kinds.php` reproduces that
commit's `kind.rs` with an empty diff.

ext/sharp/sharp_unit.h   <- SHARP_MAGO_COMMIT a59162f
…-values

# Conflicts:
#	ext/sharp/sharp_unit.h
@heyJordanParker
heyJordanParker merged commit 694e6f2 into master Oct 9, 2026
7 checks passed
heyJordanParker added a commit that referenced this pull request Oct 10, 2026
…010d53

Master pins Mago a59162f, the merge of mago-sharp#110, and this branch pinned b68a9c4, the head of mago-sharp#101. Neither commit holds the other's checker. Mago master 4010d53, the merge of mago-sharp#113, holds #101 and #110, so the merge pins it. The header that the bridge at 4010d53 builds matches this file exactly, so only `SHARP_MAGO_COMMIT` moves and `SHARP_UNIT_ABI` is unchanged.

ext/sharp/
  sharp_unit.h   SHARP_MAGO_COMMIT is 4010d53
@heyJordanParker
heyJordanParker deleted the feat/sharp-template-values branch November 3, 2026 22:08
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