Skip to content

Missing splat collections #1

@withinboredom

Description

@withinboredom

A way I've seen to enforce types with arrays is something like the psalm method + class method:

/**
 * @template T of Item
 */
class SplatCollection {
  public function append(Item ...$items) {
    array_push($this->iterator, ...$items);
  }
}

$collection->append(...$items);

I have no idea what the performance penalty is for using splats like this, but this is what the output is when added to the phpbench:

PHPBench (1.2.5) running benchmarks... #standwithukraine
with configuration file: /app/phpbench.json
with PHP version 8.1.4, xdebug ❌, opcache ✔

\App\Tests\TypHintBench

    benchWithoutType........................R2 I88 - Mo107.894μs (±0.49%)
    benchWithArrayType......................R2 I53 - Mo108.076μs (±0.47%)
    benchWithMixedType......................R1 I62 - Mo103.304μs (±0.47%)
    benchWithClassType......................R1 I60 - Mo153.612μs (±0.42%)
    benchWithSplatType......................R1 I86 - Mo167.429μs (±0.31%)

Subjects: 5, Assertions: 0, Failures: 0, Errors: 0
+--------------+--------------------+-----+------+-----+-----------+-----------+--------+
| benchmark    | subject            | set | revs | its | mem_peak  | mode      | rstdev |
+--------------+--------------------+-----+------+-----+-----------+-----------+--------+
| TypHintBench | benchWithoutType   |     | 1000 | 100 | 612.464kb | 107.894μs | ±0.49% |
| TypHintBench | benchWithArrayType |     | 1000 | 100 | 612.464kb | 108.076μs | ±0.47% |
| TypHintBench | benchWithMixedType |     | 1000 | 100 | 612.464kb | 103.304μs | ±0.47% |
| TypHintBench | benchWithClassType |     | 1000 | 100 | 612.464kb | 153.612μs | ±0.42% |
| TypHintBench | benchWithSplatType |     | 1000 | 100 | 612.464kb | 167.429μs | ±0.31% |
+--------------+--------------------+-----+------+-----+-----------+-----------+--------+

so it seems like it might be pretty slow.

Pros:

  • type checking by php
  • static analysis by psalm

Cons:

  • slow?
  • weird to write and read

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions