xp-lang / xp-generics
PHP 的 XP 泛型
v2.1.0
2024-08-04 19:41 UTC
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
为 XP 编译器 添加 XP 泛型支持的插件。
示例
// 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
安装
在您的项目中安装了 XP 编译器后,也需要包含此插件。
$ composer require xp-framework/compiler # ... $ composer require xp-lang/xp-generics # ...
无需采取其他操作。
另请参阅
- PHP 中的泛型 - PHP Annotated(2024年8月)
- 泛型和集合的状态(2024年8月)
- XP RFC: 泛型(2007年1月)
- XP RFC: 泛型优化
- PHP RFC: 泛型
- HHVM 泛型