Skip to content

feat: interfaces hold constants and operators, and enums declare operators - #174

Merged
heyJordanParker merged 11 commits into
masterfrom
feat/interface-members
Oct 18, 2026
Merged

heyJordanParker merged 11 commits into
masterfrom
feat/interface-members

Conversation

@heyJordanParker

Copy link
Copy Markdown
Owner

Problem

An interface held only methods and get-only properties, and an enum no operators. Interface constants, interface property default bodies, and operators in an interface or an enum were refused with "This class member is not supported yet in PHP#.", so every class that implements an interface repeated them.

Solution

The block Jordan approved compiles and runs:

public interface Priced
{
    const string CURRENCY = "USD";                                     // compiles: an interface constant, read as Priced.CURRENCY
    int cents { get; }                                                 // compiles: abstract, each class gives it
    string label => `${this.cents} ${Priced.CURRENCY}`;                // compiles: a default body built from the interface's own members
    static int operator <=>(Priced a, Priced b) => a.cents <=> b.cents;   // compiles: every Priced compares by cents, so a < b works
    int count { get; } = 0;                                            // compile error: an interface holds no storage, so it gives no initial value
    law cheapFirst(Priced a, Priced b) => a.cents <= b.cents || a > b;  // compile error: a law is proved against code, and any class may implement an interface later
}
public enum Size : int
{
    case Small = 1;
    case Large = 3;
    public static int operator <=>(Size a, Size b) => a.value <=> b.value;   // compiles: Size.Small < Size.Large is true
}
  • An interface constant follows a class constant's rules and is public. PHP# reads it as Priced.CURRENCY or Book.CURRENCY, and plain PHP as Priced::CURRENCY.
  • An interface operator is written static alone, as every interface member is written bare. Every overloadable operator of section 19 may be declared, with the class rules: == needs hash(), and <=> gives <, <=, > and >=.
  • The interface's trait Priced\Defaults holds each operator twice. op_Comparison is the name the engine's sortedBy and Set equality look up, and a class's own operator replaces it. 'op_Comparison@App\Priced' is the name no class replaces. A comparison of two Priced values runs $a::{'op_Comparison@App\Priced'}($a, $b) on the class of the left operand, which runs once. == holds both operands and lifts null over them.
  • A class's own <=> for two of its values wins. Two Rares order by Rare's, and two Priced values by the interface's.
  • An enum declares operators as a class does. Size.Small < Size.Large compiles only because Size declares <=>. A data enum that declares == compares its cases by it instead of by value.
  • int count { get; } = 0; in an interface is "An interface holds no storage, so it gives no initial value.", and { get => …; } names the computed form string label => …;.
  • fix: a PHP form PHP# never has is refused for good, with what PHP# writes instead #166 landed first, so the merge drops its not-supported-yet arms for interface constants and operators and enum operators. The arm keeps only the interface property forms still undecided, as { get; init; }. fix: a PHP form PHP# never has is refused for good, with what PHP# writes instead #166's law refusal is the one law arm.

Verification

  • cargo test on mago-semantics, mago-codex, mago-analyzer and mago-sharp-bridge: every target passes, with --test sharp at 428, 1036 and --test lower at 450.
  • cargo clippy -p mago-codex -p mago-semantics -p mago-analyzer -p mago-sharp-bridge --all-targets --all-features -- -D warnings: clean.
  • sharp/bin/spec: "Checked 239 examples, ignored 1, and 0 fail.", with the new interface-constants-and-operators, interface-members-in-classes and enum-operators examples among them.
  • sharp/bin/test --opcache --repeat --passes on interface_members.phpt and interface_members_preload.phpt: both pass without opcache, with the file cache, with shared memory twice in one process, under each optimizer pass alone, and with the tracing and the function JIT.
  • sharp/bin/test Zend/tests/sharp: 377 passed.

…ators

An interface could hold only methods and get-only properties, and an enum
no operators. Constants, default property bodies and operators in either
were refused as not supported yet, so every class repeated them.

Declare constants and operators on an interface:
- An interface constant follows a class constant's rules and is public,
  read as Priced.CURRENCY, also through any class that implements it
- An interface operator, written `static` alone, applies to every class
  that implements the interface, and a class's own operator for two of its
  values wins over it
- The interface's trait of default bodies holds each operator twice: by
  its own name, which the engine's sortedBy and Set equality look up and a
  class's own operator replaces, and by the interface's name, which a call
  on two values of the interface runs on the class of the left one
- Plain PHP gets the operator and the default property body through
  `use Priced\Defaults;`, and reads Priced::CURRENCY

Give an interface property a default body, and no storage:
- `string label => ...;` is the default body each class gets unless it
  overrides it
- An initial value is an error, "An interface holds no storage, so it gives
  no initial value.", and an accessor body names the computed form
- A law in an interface is an error, because a law is proved against the
  code of a class or an enum

Order and compare an enum's cases by its own operators:
- Size.Small < Size.Large compiles when Size declares `<=>`, and stays an
  error for an enum that declares none
- An enum that declares `==` compares its cases by it instead of by value

Zend/tests/sharp/
├── InterfaceMembers/
│   ├── Priced.sharp                 <- added, an interface with a constant, a default body and <=>
│   ├── Book.sharp                   <- added, a class that gets every default
│   ├── Rare.sharp                   <- added, a class with its own label and <=>
│   ├── Size.sharp                   <- added, an enum that declares <=>
│   ├── Coin.sharp                   <- added, a data enum that declares == and hash()
│   ├── Measure.sharp                <- added, an interface with ==, + and unary -
│   ├── Weight.sharp                 <- added, a class that gets Measure's operators
│   └── Report.sharp                 <- added, the reads each row runs
├── interface_members.inc            <- added, the rows and a plain PHP class using Priced\Defaults
├── interface_members.phpt           <- added, runs the rows
├── interface_members_preload.inc    <- added, preloads the fixture
└── interface_members_preload.phpt   <- added, runs the rows with the fixture preloaded
docs/spec.md                         <- sections 19, 20 and 22 state the rules and run the examples
sharp/harness/app/Priced.sharp       <- added, the interface the section 22 class examples implement
sharp/mago/crates/
├── semantics/src/internal/checker/
│   ├── sharp.rs                     <- accepts interface constants and operators and enum operators, refuses storage and laws in an interface
│   └── class_like/mod.rs            <- leaves a PHP# interface constant's access modifier to the PHP# check
├── semantics/tests/sharp.rs         <- the checker rows
├── codex/src/scanner/
│   ├── mod.rs                       <- copies an interface's operators into its trait of default bodies
│   └── class_like.rs                <- an enum that declares == gets no value equality
├── analyzer/src/
│   ├── expression/binary/utils.rs   <- an enum that declares an operator takes it
│   ├── expression/binary/comparison.rs <- orders the cases of an enum that declares <=>
│   └── statement/class_like/method_signature.rs <- a class's own operator overrides no interface operator
├── analyzer/tests/sharp.rs          <- the analyzer rows
├── sharp-bridge/src/
│   ├── lower.rs                     <- lowers interface constants and operators, enum operators, and the call on the left operand's class
│   └── lower/types.rs               <- names the class an operator runs on, and keeps operators out of property default bodies
└── sharp-bridge/tests/lower.rs      <- the lowering row
…bers

#166 kept interface constants, interface property bodies and operators in
an interface or an enum not supported yet, for this branch to decide. This
branch decides them, so each now has one arm: the not-supported-yet arm
keeps only the interface property forms still undecided, as
`{ get; init; }`, and #166's refusal of a law in an interface is the one
law arm.

sharp/mago/crates/semantics/
├── src/internal/checker/sharp.rs   <- one arm per form: #166's law refusal stays, the undecided list narrows
└── tests/sharp.rs                  <- #166's `abstract interface` error and refusal notes beside this branch's rows
patch-coverage found three paths no Rust test ran: `==` on two values of
an interface, an enum with data that declares its own `==`, and an
operator in a file whose first statement is an import.

sharp/mago/crates/
├── semantics/tests/sharp.rs      <- the enum operator row starts with an import
└── sharp-bridge/tests/lower.rs   <- added row: interface `==` lifts null, and a data enum's own `==` replaces value equality
The checker and the bridge choose each operator use with one rule, as C# does:
of the operators the two operand types, their parents and their interfaces
declare, the most specific one that takes both runs. Two that take both where
neither is more specific are one error at the use, which names the operator,
both interfaces and the operator to declare instead.

An interface operator is one static method of its Defaults trait, named after
the interface, and each use calls it on the trait itself, so no class, trait
collision or operand order changes which one runs. sortedBy passes the engine
the operator < chooses for its key type, and a Set and a Map key objects by the
most specific == their two classes and PHP# interfaces declare.

A plain PHP class that implements a PHP# interface with default bodies without
its Defaults trait is one error that names the trait. The ordering refusal on
an enum says to declare operator <=>, an interface states hash() bare, an
interface accessor body gets only PHP#'s message, and a bare constant that
implements an interface's says it is written public const.
…bers

is_undecided keeps the branch's one interface arm, a property in another form
than { get; }, { get; set; } and => expr;, and drops the constant arms #173
retired.
Priced's constants read an index, a ? :, a match, a template with a cast, a
case's name and a lambda, as a class constant's do since #173, in every mode
and with preloading.
A Set or a Map chose an object's == on every hash and key match by building and
lowercasing two names per PHP# interface the object implements, and threw past
32 candidates. Each class's operators ==, with the class each parameter takes,
are now found the first time the request compares one of its objects and kept
until the request ends, in a list that grows with no limit.

Adding and finding 100,000 objects of a class that implements two PHP#
interfaces in a Set, debug builds, best of five:

    master a70fc63      add  70.1 ms  find 121.1 ms
    before this commit     add 332.0 ms  find 508.1 ms
    this commit            add  77.7 ms  find 133.2 ms

sharp_free_stamp becomes sharp_free_pointer, since both tables that use it hold
single allocations.
… members

is_undecided keeps the branch's one interface arm, a property in another form
than { get; }, { get; set; } and => expr;, beside #176's list, which moved break
with a level, |> and a partial application to unexpected_part.
A tie of two interfaces' operators is one error on each use that takes the
class on both sides: <, +, sortedBy and contains. An operand of a class the
codebase does not hold declares no operator. The arms no input reaches, a tie
recomputed as no tie, a tie of one operator, a Defaults trait with no interface
and a sortedBy with other than one type argument, are unreachable! with their
reasons.
A Set or a Map of a class that two interfaces' operator == tie on was accepted,
as was a Set of a struct that holds one, since a class that inherited any
op_Equality counted as a value. Every == on two of them is refused, and at run
time no single == takes them, so two equal ones never matched. The key check
now asks the operator rule every == uses: a tied class is the tie error, and a
struct that holds one can't be in a Set or key a Map.

A test lowers that struct's generated ==, which has no operator to run on the
tied property. A class the codebase does not hold declares no operator, in one
iterator with the others.
@heyJordanParker
heyJordanParker merged commit f432cb7 into master Oct 18, 2026
24 checks passed
@heyJordanParker
heyJordanParker deleted the feat/interface-members 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