On Thu, 12 Mar 2020 at 03:14, Mike Schinkel <m...@newclarity.net> wrote:

> > The way I was thinking of it, and I think the way this RFC is drafted,
> the native attributes would - at least initially - be a very low-level
> feature, with libraries expected to build more useful functionality on top.
> So if a library gave meaning to << Require(SomeAttribute::class) >> then
> all consumers of that library would give that the same meaning.
>
> I read that last sentence 10 times but still am not sure what it means.
> Can you maybe give a concrete example?
>


I was thinking that a project like Doctine Annotations would still exist,
and would resolve these rules.

For instance, in Doctrine, you can specify an @Target annotation on the
annotation itself to say if it's to be used on classes, methods, etc. That
could still happen in userland, even if the base syntax was built into the
language:

use SomeAttributeLibrary\Attributes\Target;
use SomeAttributeLibrary\Reader;

<< Target('class') >>
class MyClassAttribute {}

<< MyClassAttribute >>
function foo() {}

$reader = Reader::forFunction('foo');

I haven't actually looked at the Doctrine implementation, but I imagine the
Reader code does something like this:

1) Look up the attributes on the function with
$reflectionFunction->getAttributes() (in current syntax, parse the docblock
for @ annotations)
2) Find MyClassAttribute, and look up the attributes on that
3) Find the Target attribute, and recognise it as a restriction on
MyClassAttribute
4) See that the Target specified 'class', but the usage was on a function,
and throw an error

The "required attribute" feature could be implemented the same way: the
library would provide an attribute called RequireAttribute, which took the
name of another attribute. When loading the attributes for a class, it
would look at the interfaces it implemented, and see if there were
requirements to enforce.



> > Very true. Although given the amount of parsing some tools already do
> with docblocks, if there was a popular library defining attributes like
> this, the tools would probably start picking up on it.
>
> How would they pick it up if there is no way for an interface to say "You
> must include this attribute?"
>


Tools and IDEs could have lists of the attributes used by common libraries,
and work with them just like native syntax. This already happens, for
instance this PHPStorm plugin will read Doctrine's @Target annotations and
"know" whether you're using them in the right places:
https://plugins.jetbrains.com/plugin/7320-php-annotations

Obviously, real syntax would in some ways be better, but one of the nice
things about annotations/attributes is that they provide a very flexible
mechanism for defining new features.


Regards,
-- 
Rowan Tommins
[IMSoP]

Reply via email to