https://wiki.dlang.org/?title=DIP6&feed=atom&action=historyDIP6 - Revision history2024-03-28T08:52:46ZRevision history for this page on the wikiMediaWiki 1.31.2https://wiki.dlang.org/?title=DIP6&diff=1624&oldid=prevJmdavis: Migrating DIP6 from old wiki2013-01-17T05:20:47Z<p>Migrating DIP6 from old wiki</p>
<p><b>New page</b></p><div>{| class="wikitable"<br />
!Title: <br />
!'''Annotations'''<br />
|-<br />
|DIP: <br />
|6<br />
|-<br />
|Version:<br />
|2<br />
|-<br />
|Status:<br />
|'''Approved'''<br />
|-<br />
|Created:<br />
|2009-08-01<br />
|-<br />
|Last Modified:<br />
|2009-08-21<br />
|-<br />
|Author:<br />
|Ary Borenszweig <ary<br />
|-<br />
|Links:<br />
|[[DIP6/Archive]] — [http://prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP6 Original Prowiki Page]<br />
|-<br />
|Supercedes:<br />
|[[DIP4|DIP4 (Properties)]]<br />
|}<br />
<br />
<div style="padding: 1ex 1ex; background: #ffd;">Note: This page should be reviewed and updated to reflect the actual syntax used for annotations. This page has already been archived and the version increased.</div><br />
<br />
== Abstract ==<br />
Extend the D programming language with annotations. Annotations are metadata attached to declarations and variables that can later be queried at compile-time or runtime. This metadata can't otherwise be expressed except by using boilerplate code or separate files that explicitly state which declaration have which metadata, but the process is ad-hoc and not standarized. <br />
<br />
Annotations already exist in other languages:<br />
<br />
Java: http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html<br />
<br />
C# (named "atrributes"): http://msdn.microsoft.com/en-us/library/aa287992(VS.71).aspx<br />
<br />
== Rationale ==<br />
Annotations will allow several things: reduce the amount of keywords and extend the language more easily.<br />
<br />
By reducing the ammount of keywords programmers can use more declarative names for their variables. It also reduces lexer and parser changes when upgrading the language.<br />
<br />
The language can be extended more easily just by adding new annotation names instead of new keywords: new keywords might render old code not compilable, while with new annotations that can never happen.<br />
<br />
General-purpose attributes allow many other things: specify how serialization should be done, how a field should be stored in a database, how a method maps to an http request, whether a method should do security checks before executing, whether a method should run as a test and what's the thrown exception is.<br />
<br />
== Description ==<br />
The idea is to introduce annotations in the D language. As a first stage, users won't be able to define annotations: some well-known annotations will exist and that's it.<br />
<br />
Proposed syntax: @annotation(arguments). This is what Java uses. Other syntax can be [annotation(arguments)]. This is what C# uses. Java's syntax has the advantage of having to type less when the annotation has no arguments: @annotation vs. [annotation].<br />
<br />
It can be applied to declarations:<br />
<syntaxhighlight lang="d"><br />
@annotation<br />
int x;<br />
<br />
@annotation<br />
class Foo { }<br />
</syntaxhighlight><br />
<br />
== Usage ==<br />
Annotations could be used for several things now. For example:<br />
<syntaxhighlight lang="d"><br />
@align(1)<br />
struct Foo { }<br />
<br />
@extern('C')<br />
int foo();<br />
<br />
@pure<br />
int foo() { }<br />
<br />
@nothrow<br />
int foo() { }<br />
<br />
@shared<br />
int x;<br />
<br />
@__gshared<br />
int x;<br />
<br />
@__thread<br />
int x;<br />
<br />
@final<br />
class Foo { }<br />
<br />
@abstract<br />
class Foo { }<br />
<br />
class Foo {<br />
<br />
@deprecated<br />
void foo() { }<br />
}<br />
<br />
class Foo {<br />
<br />
@override<br />
void foo() { }<br />
}<br />
</syntaxhighlight><br />
<br />
And also for new things:<br />
<br />
<syntaxhighlight lang="d"><br />
class Foo {<br />
<br />
@property<br />
int x() { ... }<br />
<br />
@property<br />
int x(int value) { ... }<br />
<br />
}<br />
</syntaxhighlight><br />
<br />
== Compiler Implementation ==<br />
At a first stange, the compiler will parse the annotations and attach them to declarations. In the semantic pass the annotations are inspected and actions are taken. For example if the @override property is found in a function, a check is made to see if it indeed overrides a base function. If @deprecated is found and deprecated features are not allowed, a warning/error is issued.<br />
<br />
If the annotation is not recognized by the compiler, an error happens (much like what happens with __traits(somehthing, foo) now: if the first argument is not recognized, it is an error). Also at this first stage, a user cannot define new annotations. A user cannot inspect annotations. The first stage will just help introducing annotations, making the language easier to evolve (no need to introduce new keywords for every new functionality). A future DIP should define how annotations evolve from this point once they have been implemented.<br />
<br />
== Copyright ==<br />
This document has been placed in the Public Domain.<br />
<br />
[[Category: DIP]]</div>Jmdavis