# - True Annotations

- Thread: https://externals.io/message/64722
- Original archive: https://news-web.php.net/php.internals/64722
- Started by Clint Priest on 2013-01-09T10:53:12+00:00
- Messages: 30
- RFC: Annotations (Declined) — https://externals.io/rfc/annotations.md
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/64722.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #64722 — Clint Priest — 2013-01-09T10:53:12+00:00

URL: https://externals.io/message/64722#64722

Just starting a new thread here to discuss true annotations vs a 
DocBlock Parser:

RFC Referenced:

https://wiki.php.net/rfc/annotations



> 
> > […]
> Why would this not work? I'm struggling to think of a place where one
> would want to use an annotation where it could be misinterpreted as an
> array literal.  If anything, the visual "conflict" or association with
> the array syntax is a good thing in my book: my brain parses it as an
> array of one or more annotations.
I agree here, I think the above, if possible would be best.  In my mind 
annotations should proabably be limited in scope to class declarations 
and thus only before a class keyword, before a property or method 
declaration.

In none of those scopes would [ ] be a parsing issue I believe...

The one case would be at the beginning of a class, but if simply added 
something such as:
[:SomeAttribute(xyz,abc),SomeAttribute2]

It could never be confused with short array syntax and is still brief.

-- 
-Clint

---

## #64724 — Derick Rethans — 2013-01-09T11:54:39+00:00

URL: https://externals.io/message/64722#64724 · In reply to: #64722

> Just starting a new thread here to discuss true annotations vs a DocBlock
> Parser:
> 
> RFC Referenced:
> 
> https://wiki.php.net/rfc/annotations
> 
> 
> 
> > […]
> I agree here, I think the above, if possible would be best.  In my mind
> annotations should proabably be limited in scope to class declarations and
> thus only before a class keyword, before a property or method declaration.
> 
> In none of those scopes would [ ] be a parsing issue I believe...
> 
> The one case would be at the beginning of a class, but if simply added
> something such as:
> [:SomeAttribute(xyz,abc),SomeAttribute2]

I've never ever seen an annotation like this in a docblock. What kind of 
wacky syntax is this?! And will we now need a parser for docblocks in 
style "phpdocumenter" and those annotations above‽

cheers,
Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug
Posted with an email client that doesn't mangle email: alpine

---

## #64726 — Christian Kaps — 2013-01-09T11:57:37+00:00

URL: https://externals.io/message/64722#64726 · In reply to: #64722

Hi,

> I agree here, I think the above, if possible would be best.  In my
> mind annotations should proabably be limited in scope to class
> declarations and thus only before a class keyword, before a property
> or method declaration.
>
> In none of those scopes would [ ] be a parsing issue I believe...
>
> The one case would be at the beginning of a class, but if simply
> added something such as:
> [:SomeAttribute(xyz,abc),SomeAttribute2]
>
> It could never be confused with short array syntax and is still 
> brief.

I think when implementing real annotations, then it should be possible 
to declare arrays in it. So I think the square brackets don't work.
[Foo([1,2,3])]

Cheers,
Christian

---

## #64727 — Ivan Enderlin @ Hoa — 2013-01-09T11:59:51+00:00

URL: https://externals.io/message/64722#64727 · In reply to: #64722

Hi internals,

I would like to give you my thoughts about annotations.

An annotation *must not change* the code *behavior* but add a useful 
*information* for the users or *tools*.

The syntax used most of the time is very restrictive (such as 
Foo('Bar'), DateType('datetime'), MinLength(42) etc.). We cannot express 
any logic formula, disjunction, conjunction or anything else. There are 
other *annotation* and *specification* languages, that exist for PHP and 
that are much more complete. So, the referenced RFC is very very 
restrictive. The purpose of annotations could be larger than "weakly 
type" some data, because it can contain informations of very different 
natures.

Fortunately, there is a common syntax between all existing ones. In Java 
or PHP, we use the @ symbol to introduce a keyword, followed by a 
description. The set of keywords with their descriptions is an 
annotation. For instance: @param, @return, @requires, @ensures, 
@throwable etc. The @ symbol can be different but it is not the purpose 
for now. So, if PHP would propose an API, it would be a way to get the 
description of a keyword. Something like: 
$reflectionAnnotation->getValue('requires') for example, or 
$reflectionAnnotation['requires']. Then, *it is part* to the user or the 
tool to *analyse* (maybe to parse) the description of the keyword to 
give it a *semantics*. It would be a compromise between all existing 
implementations and languages, but it will not close the door to more 
sophisticated annotation or specification languages.
The fact that it is part to the user or the tool to analyse the 
description is not a bad thing. Analyzing annotations should be static, 
not dynamic (i.e. during the runtime). Maybe there are different points 
of view but as annotations must not change the code behavior, runtime 
use cases are very rare.

Then, there is also a confusion between *API documentation* (not just 
simple documentation) and annotations. API documentation is an 
annotation. It does not change the code behavior, it is a useful 
information for users and tools, so *it is an annotation*. There is 
different API documentation syntaxes (or languages), but most of them 
also introduce descriptions by the @ symbol combined with a keyword, so 
it respects my previous words.

As an example of what kind of annotations we are able to have: 
contracts. This is a more complex example of a specification. It does 
not change the code behavior, it is a very important information for the 
user and some tools that automate the test process (by making 
contract-based testing or the design-by-contract paradigm).

So, to summarize myself, I propose that PHP has a native parser for the 
following annotation grammar (not in a formal syntax):

     @keyword description;

Maybe, a description can be a block, thus we have:

     @keyword { description }

And in the description, we could have other keywords:

     @keyword {
         @key description;
         @word description;
     }

It also could be useful to label/index the keyword, maybe by using the 
following syntax:

     @keyword[label] description;

It could fit the most existing use cases around without closing the door 
to "exotic" (i.e. not common) ones, such as contracts or alike 
specifications.

Best regards.




> Just starting a new thread here to discuss true annotations vs a 
> DocBlock Parser:
>
> RFC Referenced:
>
> https://wiki.php.net/rfc/annotations
>
>
> 
> > […]
> I agree here, I think the above, if possible would be best.  In my 
> mind annotations should proabably be limited in scope to class 
> declarations and thus only before a class keyword, before a property 
> or method declaration.
>
> In none of those scopes would [ ] be a parsing issue I believe...
>
> The one case would be at the beginning of a class, but if simply added 
> something such as:
> [:SomeAttribute(xyz,abc),SomeAttribute2]
>
> It could never be confused with short array syntax and is still brief.
>

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
http://disc.univ-fcomte.fr/ and http://www.inria.fr/

Member of HTML and WebApps Working Group of W3C
http://w3.org/

---

## #64728 — Yahav Gindi Bar — 2013-01-09T12:03:08+00:00

URL: https://externals.io/message/64722#64728 · In reply to: #64726

On Wed, Jan 9, 2013 at 1:57 PM, Christian Kaps <christian.kaps@mohiva.com>wrote:

> Hi,
>
>
>  I agree here, I think the above, if possible would be best.  In my
> > […]
>
> I think when implementing real annotations, then it should be possible to
> declare arrays in it. So I think the square brackets don't work.
> [Foo([1,2,3])]
>
> Cheers,
> Christian
>
> --
> 
> 
>
> But the colon prefix won't cover this case?

You can, as I've mentioned in my post at the doc-block parser discussion,
use a keyword inside the brackets to symbolize annotation, for example

[metadata: Key]
[metadata: MaxLength(10)]
[metadata: Relations(["foo", "bar", "baz"])]
public $id;

---

## #64729 — Christian Kaps — 2013-01-09T12:09:05+00:00

URL: https://externals.io/message/64722#64729 · In reply to: #64728

Am 09.01.2013 13:03, schrieb Yahav Gindi Bar:
> On Wed, Jan 9, 2013 at 1:57 PM, Christian Kaps 
> <christian.kaps@mohiva.com>wrote:
>
> > […]
>
> You can, as I've mentioned in my post at the doc-block parser 
> discussion,
> use a keyword inside the brackets to symbolize annotation, for 
> example
>
> [metadata: Key]
> [metadata: MaxLength(10)]
> [metadata: Relations(["foo", "bar", "baz"])]
> public $id;

Then rather [@Foo([1,2,3])] if it's possible!

---

## #64733 — Yahav Gindi Bar — 2013-01-09T12:13:23+00:00

URL: https://externals.io/message/64722#64733 · In reply to: #64729

On Wed, Jan 9, 2013 at 2:09 PM, Christian Kaps <christian.kaps@mohiva.com>wrote:

> Am 09.01.2013 13:03, schrieb Yahav Gindi Bar:
>
> > […]
>
> Then rather [@Foo([1,2,3])] if it's possible!
>
>
>
> --
> 
> 
>
> Yeah, that's cool. To be honest, I took that idea from C# which has, for
example (Quoted from standard template AssemblyInfo.cs):

// Version information for an assembly consists of the following four
values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build
Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

---

## #64737 — Pierrick Charron — 2013-01-09T12:56:23+00:00

URL: https://externals.io/message/64722#64737 · In reply to: #64722

Annotations can be nested so in this case [Foo([BAR])] there is a big
ambiguity and we can not determine if [BAR] is an array with the BAR
constant in it or an annotation.

Pierrick


> In none of those scopes would [ ] be a parsing issue I believe...

---

## #64738 — Patrick Schaaf — 2013-01-09T13:17:44+00:00

URL: https://externals.io/message/64722#64738 · In reply to: #64737

Regarding syntax... Would this work?

<|foo|>
<|bar( <|baz|> )|>

best regards
  Patrick

---

## #64739 — Nikita Nefedov — 2013-01-09T13:27:26+00:00

URL: https://externals.io/message/64722#64739 · In reply to: #64738

No please, two symbols for each side looks ugly.
BTW There's number sign (#) which is, as far as I remember, not used in  
PHP at all. Could be something like:
#JoinColumn(name="...", type="...", ...)
#Foo(Bar())

Or

#Foo(#Bar())

(should we put a "annotation-sign" in front of a nested annotation?)



> Regarding syntax... Would this work?
>
> <|foo|>
> <|bar( <|baz|> )|>
>
> best regards
>   Patrick

---

## #64740 — Clint Priest — 2013-01-09T13:28:30+00:00

URL: https://externals.io/message/64722#64740 · In reply to: #64728

We should probably be referring to this type of syntax as attributes 
rather than annotations since annotations are currently defined in 
docblock comments and are recognized by certain software and utilities.

Whereas annotations have no effect on compilation or at runtime, 
attributes do have the possibility of affecting compilation or runtime.

I like the idea of having a schema parameter like metadata: as that 
would allow php to reserve php: for example.  Almost like a namespace, 
in fact it could actually be a namespace, such as:


class a {
     [php:Param($s) = StringLength(4, ErrorMessage='Parameter $s length 
cannnot exceed 4.', ErrorLevel=E_USER_ERROR)]
     public function foo($s) { ... ]
}

I also think that providing no schema should be allowed as well, such as:

[: ... ]

We could, again, model this after the C# model whereby user-land 
attributes may be defined, PHP could also provide it's own standard ones.

The only reason I mention C# here is that it's been in wide use for some 
time now.


> On Wed, Jan 9, 2013 at 1:57 PM, Christian Kaps <christian.kaps@mohiva.com>wrote:
>
> > […]
> You can, as I've mentioned in my post at the doc-block parser discussion,
> use a keyword inside the brackets to symbolize annotation, for example
>
> [metadata: Key]
> [metadata: MaxLength(10)]
> [metadata: Relations(["foo", "bar", "baz"])]
> public $id;
>

-- 
-Clint

---

## #64742 — Pierre du Plessis — 2013-01-09T13:29:33+00:00

URL: https://externals.io/message/64722#64742 · In reply to: #64739

The # would be parsed as a comment


Kind Regards
Pierre du Plessis

*Cell*: 072 775 3477
*Fax*: 086 650 4991
*Email*: info@customscripts.co.za <admin@customscripts.co.za>
*www*: http://www.customscripts.co.za




> No please, two symbols for each side looks ugly.
> BTW There's number sign (#) which is, as far as I remember, not used in
> PHP at all. Could be something like:
> #JoinColumn(name="...", type="...", ...)
> #Foo(Bar())
>
> Or
>
> #Foo(#Bar())
>
> (should we put a "annotation-sign" in front of a nested annotation?)
>
>
> 
>
>  Regarding syntax... Would this work?
> > […]

---

## #64741 — Pierrick Charron — 2013-01-09T13:30:05+00:00

URL: https://externals.io/message/64722#64741 · In reply to: #64739

# is an alternative syntax for comments


> #Foo(#Bar())

---

## #64744 — Benjamin Eberlei — 2013-01-09T14:58:21+00:00

URL: https://externals.io/message/64722#64744 · In reply to: #64722

I think this RFC syntax is outdated. We can remove the whole new syntax and
just make everything between <> php code that returns the last statement
because of the array short syntax this ends up to be stuff like:

<['foo' => bar']>
<['foo' => foo()]>
<['foo' => new Foo('bar')]>

This would greatly simplify the feature, because then its just PHP code and
everybody allready knows that.




> Just starting a new thread here to discuss true annotations vs a DocBlock
> Parser:
>
> RFC Referenced:
>
> https://wiki.php.net/rfc/**annotations<https://wiki.php.net/rfc/annotations>
>
>
> 
>
> > […]
> I agree here, I think the above, if possible would be best.  In my mind
> annotations should proabably be limited in scope to class declarations and
> thus only before a class keyword, before a property or method declaration.
>
> In none of those scopes would [ ] be a parsing issue I believe...
>
> The one case would be at the beginning of a class, but if simply added
> something such as:
> [:SomeAttribute(xyz,abc),**SomeAttribute2]
>
> It could never be confused with short array syntax and is still brief.
>
> --
> -Clint

---

## #64745 — Levi Morrison — 2013-01-09T15:15:16+00:00

URL: https://externals.io/message/64722#64745 · In reply to: #64722

> https://wiki.php.net/rfc/annotations

Perhaps I am blind, but I do not see where in the RFC is defends its
choice to use `<>`. Every other language I know of uses `@`, and I do
not know of technical reasons why we couldn't use the same symbol.
Annotations wouldn't be able to contain expressions so there wouldn't
be anything that could generate a suppressible error.

---

## #64747 — Benjamin Eberlei — 2013-01-09T15:18:40+00:00

URL: https://externals.io/message/64722#64747 · In reply to: #64745

Every other language used :: or . for namespaces. The problem with @ is its
definition as error suppression operator, and Guilherme and Pierrick didn't
come up with a solution to stay BC using the @. Additionally some other
languages use [] (which also doesnt work in PHP).


On Wed, Jan 9, 2013 at 4:15 PM, Levi Morrison <morrison.levi@gmail.com>wrote:

> > […]
>
> Perhaps I am blind, but I do not see where in the RFC is defends its
> choice to use `<>`. Every other language I know of uses `@`, and I do
> not know of technical reasons why we couldn't use the same symbol.
> Annotations wouldn't be able to contain expressions so there wouldn't
> be anything that could generate a suppressible error.

---

## #64748 — Anthony Ferrara — 2013-01-09T15:20:43+00:00

URL: https://externals.io/message/64722#64748 · In reply to: #64745

Levi,

On Wed, Jan 9, 2013 at 10:15 AM, Levi Morrison <morrison.levi@gmail.com>wrote:

> > […]
>
> Perhaps I am blind, but I do not see where in the RFC is defends its
> choice to use `<>`. Every other language I know of uses `@`, and I do
> not know of technical reasons why we couldn't use the same symbol.
> Annotations wouldn't be able to contain expressions so there wouldn't
> be anything that could generate a suppressible error.


Think about a function declaration:

@param("bar")
 function foo($bar) {}

What's the parser supposed to do there? Is it an annotation? Or an
error-silenced function call?

Granted, leaving off the `;` may make it possible to parse without
ambiguity (since the @ - Const String - ( - ... - ) sequence, followed by a
function declaration direclty may be possible)...

But it's ambiguous at best (especially to read)...

Anthony

---

## #64749 — Vladislav Veselinov — 2013-01-09T15:21:25+00:00

URL: https://externals.io/message/64722#64749 · In reply to: #64745

> be anything that could generate a suppressible error.

Not true:

@ORM\Column()
class Test() {

}

What if there's a function in the ORM namespace called Column.
Is this a supressed error of a function call and we've missed the ";"
or is it an annotation?

---

## #64750 — Levi Morrison — 2013-01-09T15:21:48+00:00

URL: https://externals.io/message/64722#64750 · In reply to: #64748

> Levi,
>
> On Wed, Jan 9, 2013 at 10:15 AM, Levi Morrison <morrison.levi@gmail.com>
> wrote:
> > […]
>
>
> Think about a function declaration:
>
> @param("bar")
>  function foo($bar) {}
>
> What's the parser supposed to do there? Is it an annotation? Or an
> error-silenced function call?
>
> Granted, leaving off the `;` may make it possible to parse without ambiguity
> (since the @ - Const String - ( - ... - ) sequence, followed by a function
> declaration direclty may be possible)...
>
> But it's ambiguous at best (especially to read)...
>
> Anthony

Maybe I'm a complete fool, but since annotations aren't executed (they
are declarative only), this should cause no problems.

---

## #64751 — Anthony Ferrara — 2013-01-09T15:27:49+00:00

URL: https://externals.io/message/64722#64751 · In reply to: #64750

Levi

Maybe I'm a complete fool, but since annotations aren't executed (they
> are declarative only), this should cause no problems.
>

You're not a fool. And the point is not that they are executed, but because
they are nearly syntactically identical to executable code. So parser and
readability wise they can be easily confused. Which is the problem...

Anthony

---

## #64752 — Levi Morrison — 2013-01-09T15:31:01+00:00

URL: https://externals.io/message/64722#64752 · In reply to: #64751

> they are nearly syntactically identical to executable code.

"nearly" is the keyword there. They lack the ending semi-colon. Sure,
this might mean PHP has to actually use an abstract syntax tree, but
that's long overdue in my opinion. I know others disagree. This is
only tangentially related, so I won't say more.

---

## #64754 — Larry Garfield — 2013-01-09T16:20:52+00:00

URL: https://externals.io/message/64722#64754 · In reply to: #64752

> > […]
>
> "nearly" is the keyword there. They lack the ending semi-colon. Sure,
> this might mean PHP has to actually use an abstract syntax tree, but
> that's long overdue in my opinion. I know others disagree. This is
> only tangentially related, so I won't say more.

Even if the parser is adjusted to cope with that, it's still an 
extremely subtle difference for developers to keep track of.  Consider 
the following procedural snippets:

@Foo[narf]
function something($narf) {}

@Foo[narf];
function something($narf) {}

The first one says that $narf should pass @Foo validation.

The second one says to call the function Foo with the string constant 
"narf", and then define a function called something().

That's the sort of subtle bug that's just begging for someone to make an 
honest typo and spend the next 6 hours tracking it down, until he 
realizes what he did and proceeds to bang his head against his desk in 
frustration for missing such a "simple" error.

Let's not subsidize the headache drug manufacturers with PHP syntax 
decisions. :-)

--Larry Garfield

---

## #64755 — Levi Morrison — 2013-01-09T16:24:18+00:00

URL: https://externals.io/message/64722#64755 · In reply to: #64754

> Let's not subsidize the headache drug manufacturers with PHP syntax
> decisions. :-)

Too late.

---

## #64756 — Larry Garfield — 2013-01-09T16:27:51+00:00

URL: https://externals.io/message/64722#64756 · In reply to: #64733

Before we all bikeshed on the syntax, shouldn't we be figuring out the 
feature-set first?

Over in PHP-FIG, we've found it useful to audit the existing market to 
see what's in use.  That doesn't dictate decisions we make, but it can 
be instructional.  Eg, if we find that most annotation-using projects 
out there currently support and use arrays in annotations, then any core 
implementation really needs to support that as well in order to be a 
reasonable replacement.  Maybe not in the same way, but feature-parity 
with the current market is the bare-minimum for any such core feature to 
be successful.  It could certainly go farther, but it cannot go less-far.

--Larry Garfield

---

## #64757 — Rafael Dohms — 2013-01-09T16:29:52+00:00

URL: https://externals.io/message/64722#64757 · In reply to: #64754

On Wed, Jan 9, 2013 at 5:20 PM, Larry Garfield <larry@garfieldtech.com>wrote:

> 
>
> > […]
>
> Even if the parser is adjusted to cope with that, it's still an extremely
> subtle difference for developers to keep track of.  Consider the following
> procedural snippets:
>
> @Foo[narf]
> function something($narf) {}
>
> @Foo[narf];
> function something($narf) {}
>
> The first one says that $narf should pass @Foo validation.
>
> The second one says to call the function Foo with the string constant
> "narf", and then define a function called something().
>
> That's the sort of subtle bug that's just begging for someone to make an
> honest typo and spend the next 6 hours tracking it down, until he realizes
> what he did and proceeds to bang his head against his desk in frustration
> for missing such a "simple" error.
>
> Let's not subsidize the headache drug manufacturers with PHP syntax
> decisions. :-)
>
> --Larry Garfield
>
>

I agree with the risks here.

But i also really prefer a Token: <content>  approach then a [open token]
<content> [close token] approach

Can we use a combination? like:

@annot: Foo[narf]



-- 
Rafael Dohms
PHP Evangelist and Community Leader
http://doh.ms
http://wwwamsterdamphp.nl

---

Next page: https://externals.io/message/64722.md?page=2
