xp-forge / partial

Partials: Compile-time metaprogramming
0 stars 0 forks source link

Handle transformations and regular traits consistently... #11

Closed thekid closed 9 years ago

thekid commented 9 years ago

...from a user perspective.

At the same time, allow "is" and "with" (next to "including" for BC) to circumvent awkward feeling formulations. See https://github.com/xp-forge/partial/issues/10#issuecomment-143543442

Example 1

use lang\partial\Identity;
use lang\partial\Comparators;

class Name extends \lang\Object {
  use Name\is\Identity;
  use Name\with\Comparators;

}

Example 2

use lang\partial\ListOf;

class Customers extends \lang\Object {
  use Customers\is\ListOf;

}

Implementation

The inner workings to achieve consistent handling are the special case handling of traits:

if ($transform->kind()->isTrait()) {
  class_alias(strtr($transform->name(), '.', '\\'), $class);
} else {
  $body= $transform->constructor()->newInstance()->transform($mirror);
  $code= // Generation omitted for brevitiy
  eval($code);
}

Note: Use Identity inside the class will continue to work as before!

thekid commented 9 years ago

:heart: - this PR solves the handling issue IMHO, doesn't break BC, is extensible and doesn't come with a horrible overhead. Think I'll go for it.