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

Typesafe implementations #15

Open
thekid opened this issue Oct 21, 2021 · 0 comments
Open

Typesafe implementations #15

thekid opened this issue Oct 21, 2021 · 0 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@thekid
Copy link
Member

thekid commented Oct 21, 2021

A typical usecase is to load classes from a given package by a given name:

use lang\reflection\Package;

$type= (new Package('xp.xar.instruction'))->type(ucfirst($name).'Instruction');

This loads e.g. the class ExtractInstruction, which extends an abstract base class called Instruction (or implements an interface of that name). However, the above does not guarantee this from a type perspective, a class NotInstruction may exist that does not fulfill this contract.

Instead of having to add a check (using isSubclassOf(), e.g.), a new API should encapsulate this:

use lang\reflection\Implementations;
use xp\xar\Instruction;

$type= (new Package('xp.xar.instruction'))->implementationsOf(Instruction::class)->type(ucfirst($name).'Instruction');
$type= Implementations::of(Instruction::class)->type('xp.xar.instruction.'.ucfirst($name).'Instruction');
$type= Implementations::of(Instruction::class)->in('xp.xar.instruction')->type(ucfirst($name).'Instruction');
@thekid thekid added enhancement New feature or request question Further information is requested labels Oct 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant