xp-lang / xp-generics
XP generics for PHP
Installs: 1 275
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 1
Requires
- php: >=7.4.0
- xp-framework/compiler: ^9.0 | ^8.8
- xp-framework/core: ^12.0 | ^11.4
Requires (Dev)
- xp-framework/reflection: ^3.0 | ^2.14
- xp-framework/test: ^2.0 | ^1.0
README
Plugin for the XP Compiler which adds support for XP generics.
Example
// Declaration namespace com\example; class PriorityQueue<E> { private $elements; private $comparator= null; private $sorted= true; public function __construct(E... $elements) { $this->elements= $elements; } public function comparing(?function(E, E): int $comparator): self { $this->comparator= $comparator; return $this; } public function push(E $element): void { $this->elements[]= $element; $this->sorted= false; } public function pop(): ?E { if (!$this->sorted) { $this->comparator ? usort($this->elements, $this->comparator) : sort($this->elements); $this->sorted= true; } return array_pop($this->elements); } } // Usage $q= new PriorityQueue<string>(); $q->push('Test'); $q->push(123); // lang.IllegalArgumentException
Installation
After installing the XP Compiler into your project, also include this plugin.
$ composer require xp-framework/compiler # ... $ composer require xp-lang/xp-generics # ...
No further action is required.
See also
- Generics in PHP??? - PHP Annotated from August 2024
- State of Generics and Collections from August 2024
- XP RFC: Generics from January 2007
- XP RFC: Generics optimization
- PHP RFC: Generics
- HHVM Generics