]> git.llucax.com Git - personal/website.git/blob - source/blog/posts/2009/11/13-annotations--properties-and-safety-are-coming-to-d.rst
Update resume with The Podcast App
[personal/website.git] / source / blog / posts / 2009 / 11 / 13-annotations--properties-and-safety-are-coming-to-d.rst
1 Title: Annotations, properties and safety are coming to D
2 Tags: en, d, annotation, property, safe, trusted
3
4 Two days ago, the documentation of functions were updated__ with some
5 interesting revelations...
6
7 __ http://www.dsource.org/projects/phobos/changeset/1329
8
9 After **a lot** of discussion about properties needing improvements [*]_, it
10 seems like they are now officially implemented using the *annotations*, which
11 seems to be official too now, after some more discussion [*]_.
12
13 Annotations will be used for another longly discussed feature, a *safe* [*]_
14 subset of the language. At first it was thought as some kind of separate
15 language, activated through a flag ``-safe`` (which is already in the DMD
16 compiler but has no effect AFAIK), but after the discussion it seems that it
17 will be part of the main language, being able to *mark* parts of the code as
18 ``safe`` or ``trusted`` (unmarked functions are ``unsafe``).
19
20 Please take a look at the discussions for the details but here are some
21 examples:
22
23 Annotations
24 -----------
25
26 They are prefixed with ``@`` and are similar to attributes__ (not class
27 attributes; ``static``, ``final``, ``private``, etc. see the specs for
28 details).
29
30 __ http://www.digitalmars.com/d/2.0/attribute.html
31
32 For example::
33
34     @ann1 {
35         // things with annotation ann1
36     }
37
38     @ann2 something; // one thing with annotation ann2
39
40     @ann3:
41         // From now on, everything has the ann3 annotation
42
43 For now, only the compiler can define annotations, but maybe in the future the
44 user can do it too and access to them using some reflection. Time will tell, as
45 usual, Walter is completely silent about this.
46
47
48 Properties
49 ----------
50
51 Properties are now marked with the ``@property`` annotation (maybe a shorter
52 annotation would be better? Like ``@prop``).  Here is an example::
53
54     class Foo {
55         @property int bar() { ... } // read-only
56         @property { // read-write
57             char baz() { ... }
58             void baz(char x) { ...}
59         }
60
61
62 Safe
63 ----
64
65 Now functions can be marked with the annotations ``@safe`` or ``@trusted``.
66 Unmarked functions are *unsafe*. Safe functions can only use a subset of the
67 language that it's *safe* by some definition (*memory safe* and *no undefined
68 behavior* are probably the most accepted definition). Here is a list of things
69 a safe function can't do:
70
71 - No casting from a pointer type to any type other than ``void*``.
72 - No casting from any non-pointer type to a pointer type.
73 - No modification of pointer values.
74 - Cannot access unions that have pointers or references overlapping with other types.
75 - Calling any unsafe functions.
76 - No catching of exceptions that are not derived from ``class Exception``.
77 - No inline assembler.
78 - No explicit casting of mutable objects to immutable.
79 - No explicit casting of immutable objects to mutable.
80 - No explicit casting of thread local objects to shared.
81 - No explicit casting of shared objects to thread local.
82 - No taking the address of a local variable or function parameter.
83 - Cannot access ``__gshared`` variables.
84
85 There is some discussion about bound-checking being active in safe functions
86 even when the ``-release`` compiler flag is used.
87
88 Trusted functions are not checked by the compiler, but trusted to be safe
89 (should be manually verified by the writer of the function), and can use
90 unsafe code and call unsafe functions.
91
92 .. [*] Here are *some* links to the property discussions:
93
94     - http://www.digitalmars.com/d/archives/digitalmars/D/property_keyword_96728.html
95     - http://www.digitalmars.com/d/archives/digitalmars/D/resolveProperties_dmd_hacking_96677.html
96     - http://www.digitalmars.com/d/archives/digitalmars/D/property_syntax_strawman_94692.html
97     - http://www.digitalmars.com/d/archives/digitalmars/D/property_getProperty_setProperty_94599.html
98     - http://www.digitalmars.com/d/archives/digitalmars/D/Property_and_method_groups_94505.html
99     - http://www.digitalmars.com/d/archives/digitalmars/D/Yet_a_new_properties_proposal_94316.html
100     - http://www.digitalmars.com/d/archives/digitalmars/D/Properties_problems_94264.html
101     - http://www.digitalmars.com/d/archives/digitalmars/D/Properties_a.b.c_3_94225.html
102     - http://www.digitalmars.com/d/archives/digitalmars/D/Properties\_--_another_one_that_gets_me_94223.html
103     - http://www.digitalmars.com/d/archives/digitalmars/D/properties_using_template_mixins_and_alias_this_87952.html
104     - http://www.digitalmars.com/d/archives/digitalmars/D/property_syntax_problems_83326.html
105     - http://www.digitalmars.com/d/archives/digitalmars/D/DIP4_Properties_93698.html
106     - http://www.digitalmars.com/d/archives/digitalmars/D/new_DIP5_Properties_2_93917.html
107     - http://prowiki.org/wiki4d/wiki.cgi?DocComments/Property
108
109 .. [*] Discussion about annotations:
110
111     - http://www.digitalmars.com/d/archives/digitalmars/D/Idea_For_Attributes_Annotations_85768.html
112     - http://www.digitalmars.com/d/archives/digitalmars/D/DIP6_Attributes_94648.html
113
114 .. [*] Discussions about SafeD:
115
116     - http://www.digitalmars.com/d/archives/digitalmars/D/SafeD_and_Nullable_concept_82152.html
117     - http://www.digitalmars.com/d/archives/digitalmars/D/Memory_safety_C_D_and_more_89441.html
118     - http://www.digitalmars.com/d/archives/digitalmars/D/safety_model_in_D_99989.html
119     - http://www.digitalmars.com/d/archives/digitalmars/D/An_interesting_consequence_of_safety_requirements_100057.html
120     - http://www.digitalmars.com/d/archives/digitalmars/D/Safety_undefined_behavior_safe_trusted_100138.html
121
122 .. vim: set et sw=4 sts=4 :