Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support generics #26

Closed
thekid opened this issue Nov 16, 2017 · 2 comments
Closed

Support generics #26

thekid opened this issue Nov 16, 2017 · 2 comments

Comments

@thekid
Copy link
Member

thekid commented Nov 16, 2017

Example

// Declaration
class Vector<T> {
  public function add(T $t): self { ... }
  public function get(int $i): T { ... }
}

// Instantiation
$vector= new Vector<string>();

See also

xp-framework/rfc#106
xp-framework/rfc#193
https://wiki.php.net/rfc/generics
https://docs.hhvm.com/hack/generics/introduction

@thekid
Copy link
Member Author

thekid commented Nov 5, 2022

Real-life example:

diff --git a/src/main/php/de/thekid/dialog/color/PriorityQueue.php b/src/main/php/de/thekid/dialog/color/PriorityQueue.php
index a683a76..6fcb295 100755
--- a/src/main/php/de/thekid/dialog/color/PriorityQueue.php
+++ b/src/main/php/de/thekid/dialog/color/PriorityQueue.php
@@ -1,12 +1,12 @@
 <?php namespace de\thekid\dialog\color;
 
 /** @test de.thekid.dialog.unittest.PriorityQueueTest */
-class PriorityQueue {
+class PriorityQueue<T> {
   private $elements= [];
   private $sorted= true;
   private $comparator= null;
 
-  public function comparing(function(mixed, mixed): int $comparator): self {
+  public function comparing(function(T, T): int $comparator): self {
     $this->comparator= $comparator;
     return $this;
   }
@@ -17,13 +17,13 @@ class PriorityQueue {
   }
 
   /** Pushes an element */
-  public function push($element): void {
+  public function push(T $element): void {
     $this->elements[]= $element;
     $this->sorted= false;
   }
 
   /** Pops an element */
-  public function pop() {
+  public function pop(): ?T {
     if (!$this->sorted) {
       $this->comparator ? usort($this->elements, $this->comparator) : sort($this->elements);
       $this->sorted= true;

Maybe as an addition xp-lang/xp-generics package?

@thekid
Copy link
Member Author

thekid commented Nov 6, 2022

Maybe as an addition xp-lang/xp-generics package?

Yes 😄 -> https://github.com/xp-lang/xp-generics/releases/tag/v0.4.0

@thekid thekid closed this as completed Nov 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant