Skip to content

Support generics - #51

Open
thekid wants to merge 5 commits into
mainfrom
feature/generics
Open

Support generics#51
thekid wants to merge 5 commits into
mainfrom
feature/generics

Conversation

@thekid

@thekid thekid commented Aug 22, 2026

Copy link
Copy Markdown
Member

This pull request adds support for XP Generics to the reflection API.

Example

use lang\{Reflection, Primitive, Generic};

#[Generic(self: 'T')]
class Sequence implements Value {
  private $elements;

  #[Generic(params: 'T...')]
  public function __construct(... $elements) {
    $this->elements= $elements;
  }

  #[Generic(params: 'T[]', return: 'self<T>')]
  public function extend($elements): self {
    foreach ($elements as $element) {
      $this->elements[]= $element;
    }
    return $this;
  }

  #[Generic(self: 'R', params: 'function(T): R', return: 'self<R>')]
  public function map($map) {
    return create("new self<$R>")->extend(array_map($map, $this->elements));
  }

  #[Generic(return: 'T[]')]
  public function elements() { return $this->elements; }
}

// Reflecting a parameterized type
$type= Reflection::type(Sequence::class);
$typpe->generic();         // false
$type->parameterized();    // ['T']

// Parameterize a parameterized type to get a generic type
$generic= $type->parameterize([Primitive::$STRING]);
$generic->generic();       // true
$generic->parameterized(); // null

// Reflecting a generic type
$generic->definition();    // (the above type)
$generic->arguments();     // [lang.Primitive<string>]

// Reflecting a parameterized method
$method= $generic->method('map');
$method->generic();         // false
$method->parameterized();  // ['R']

// Parameterize a parameterized method to get a generic method
$generic= $method->parameterize([Primitive::$INT]);
$generic->generic();       // true
$generic->parameterized(); // null

// Reflecting a generic method
$generic->definition();    // (the above method)
$generic->arguments();     // [lang.Primitive<int>]

Command integration

$ xp reflect lang.reflection.unittest.Sequence
@FileSystemCL<./src/test/php>
public class lang.reflection.unittest.Sequence<T> implements lang.Value {
  public function __construct(T... $elements)

  public function extend(T[] $elements): self
  public function map<R>(function(T): R $map): self<R>
  public function elements(): T[]
  public function hashCode(): var
  public function toString(): var
  public function compareTo(var $value): var
}

Implementation status

  • Generic definitions and their type parameters (Sequence<T>)
  • Generic types and their type arguments (Sequence<string>)
  • Generic methods - see Add support for generic methods core#372
  • Show type parameters in reflect command

See also

Comment thread composer.json
"description" : "Reflection",
"keywords": ["module", "xp"],
"require" : {
"xp-framework/core": "^12.0 | ^11.0 | ^10.13",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite a BC break; should we combine it with #50 while we're at it?

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