Skip to content

Support generic methods #3

Open
@thekid

Description

@thekid

Like the reverse() method in the following example:

// A generic definition, shortened for brevity
class List<E> {
  public function __construct(E... $elements) { }
  public function get(int $i): E $element { }
  public function size(): int { }
}

class Test {

  // Generic method declaration
  private static function reverse<E>(List<E> $list): iterable {
    for ($i= $list->size() - 1; $i >= 0; $i--) {
      yield $list->get($i);
    }
  }

  public static function main(array<string> $args): void {
    $list= new List<string>($args);

    // Call the generic method, potentially without type (as it could be inferred)
    foreach (self::reverse<string>($list) as $element) {
      Console::writeLine($element);
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions