blob: 7639741207dd66ea4aba9083a8eb9fe9a26852a3 [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001CONCEPT
2 modifiers
3
4DESCRIPTION
5 A modifier changes the syntactic and/or semantic behaviour of
6 an object-global variable or a function in an object.
7 The existing modifiers are described below.
8 To use a modifier just prepend it to the declaration. If several
9 modifiers are to be used their order does not matter:
10
11 private int bar; // example for a variable
12 protected nomask int foo() { return 3; } // example for a function
13
14 For functions:
15 ~~~~~~~~~~~~~~
16 private -- such functions can only be called with an internal
17 call from within this file. Not even inheriting
18 objects can call these functions. You can nevertheless
19 build an lfun-closure with #' out of a private function
20 (but you cannot save and restore it).
21 protected -- such functions can be called from within the object,
22 or from inheriting objects; but in neither case
23 with call_other(). It is possible to create #' closures
24 or use symbol_function() from within the object.
25 Its use is preferred over the older "static".
26 static -- such functions can be called from within the object
27 in either way (internal call or with call_other()).
28 Inheriting objects can call such functions.
29 But it is not possible to call static functions from
30 other objects via call_other().
31 The use of 'static' in new code is not recommended.
32 Note that an add_action() is treated like a call
33 from within the object except the player who got the
34 add_action() was forced (thus it is a simple way to
35 secure an add_action() against forces, although this
36 method has the severe disadvantages of raising an error
37 at the force so better use the security system).
38 Also efuns like call_out() or input_to() can call
39 these functions if given as a string.
Zesstra50c62672017-06-20 22:04:51 +020040 visible -- this is the default type. Such functions can be called
MG Mud User88f12472016-06-24 23:31:02 +020041 from within the file as well as from inheriting objects
42 and other objects via call_other().
Zesstra50c62672017-06-20 22:04:51 +020043 public -- similar to "visible", but the declaration as "public"
44 results in the impossibility to change the accessibility
45 at the inherit statement (see below). No error will
46 occur, only the type will not be modified by the inherit
MG Mud User88f12472016-06-24 23:31:02 +020047 statement.
48 nomask -- such functions cannot be overridden by inheriting
49 objects. If you have the fun foo() defined in your
50 object and inherit an object which also has declared
51 a function foo() and this nomask, you will get an
52 compile error if you try to load your object.
53 Furthermore a shadow will fail if it tries to shadow
54 a nomask declared function.
55 varargs -- this changes the syntax of the function in a way that
56 not all of the arguments in the declaration must be
57 given at the call. This is often very usefull if some
58 of the arguments shall be omitable (the omitted
59 arguments are set to 0 if the function is called with
60 fewer arguments than specified).
61 This is mainly within the object really necessary;
62 call_other()s usually (that is if they do not have a
63 certain pragma ('man pragma')) do not need the called
64 function to be declared varargs to omit any arguments,
65 but it is good style to use this modifier to document
66 the code by this.
67 deprecated - Whenever this function is called, a warning is issued.
68 Usually this is done at compile-time. Exceptions are
69 call_others and symbol_function() which warn at run-time.
70
71 For object-global variables:
72 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73 private -- such variables can only be accessed from within the
74 same object. Not even inheriting objects can access
75 private variables.
76 It is a good style to declare all internal variables
77 private to prevent inheriting objects from accessing
78 the variables directly without using functions.
79 nosave -- such variables are neither stored with save_object()
80 nor restored with restore_object(). This can be very
81 useful if you want a room to use save_object() and
82 restore_object() to save your own defined variables
83 but not the hundreds of variables inherited from a
84 room-class (e.g. /complex/room). You then use the modifier
85 at the inherit statement (see below).
86 Note that nosave and private do not overlap in any
87 way. They are absolutely independant.
88 static -- the old name for 'nosave'. Its use is deprecated.
Zesstra50c62672017-06-20 22:04:51 +020089 protected -- such variables can be accessed by inheriting
90 programs. There is little difference to the default
91 visibility ("visible"). Protected variables are
92 not seen by variable_exists() from other objects,
93 and this modifier can be detected with variable_list().
94 visible -- this is the default visibility. The variable can
95 accessed by inheriting programs.
MG Mud User88f12472016-06-24 23:31:02 +020096 public -- declares the variable public. It cannot be declared
Zesstra50c62672017-06-20 22:04:51 +020097 protected, private or static by inheriting. No error
98 will occur, only the type will not be modified by the
99 inherit statement.
MG Mud User88f12472016-06-24 23:31:02 +0200100 deprecated - Whenever this variable is used, a warning is issue.
101 Usually this is done at compile-time, but
102 symbol_variable() warns at run-time.
103
104 It is no good style to let inheriting objects have access to
105 internal variables so declare them as private and offer functions
106 to query and change the variables if possible.
107
Zesstra50c62672017-06-20 22:04:51 +0200108 Inheritance:
109 ~~~~~~~~~~~~
MG Mud User88f12472016-06-24 23:31:02 +0200110 It is also possible to redeclare all variables and/or functions
111 of an inherited object for the own object at the inheriting
112 statement:
113
114 private functions nosave variables inherit "complex/room";
115 public variables inherit "complex/room";
116 private functions inherit "complex/room";
117
118 To redeclare a function or a variable declared public in the
119 inherited object to be private or protected is not possible.
120
Zesstra50c62672017-06-20 22:04:51 +0200121 The following table shows the result of the combination of
122 visibility modifiers at the function declaration and inherit
123 statement:
124
125 Function | Inheritance modifiers
126 modifier | public | visible | static | protected | private
127 -----------+-----------+-----------+-----------+-----------+---------
128 public | public | public | public | public | public
129 visible | public | visible | static | protected | private
130 static | static | static | static | protected | private
131 protected | protected | protected | protected | protected | private
132 private | private | private | private | private | private
133
MG Mud User88f12472016-06-24 23:31:02 +0200134 There also exists a modifier explicitly for the inherit statement:
135
136 virtual -- inherits the given object virtually. This only makes
137 sense in a complex inherit tree.
138 If an object is inherited normally (not virtually)
139 twice somewhere in the inherit tree the intern
140 variables exist twice. If inherited virtually they
141 exist only once.
142 Example:
143 A inherits B and C.
144 B inherits D.
145 C inherits D.
146 If the inheritance of D is virtual in B and C
147 D's variables exist only once in A. If A changes
148 D's variables via functions of B this also changes
149 the variables of D as known by C.
150
151 virtual: non-virtual:
152 A A
153 / \ / \
154 B C B C
155 \ / | |
156 D D D
157
158
159 To simplify the adoption of existing code, LPC allows to specify
160 a default visibility for functions and variables, using a syntax
161 similar to the inherit syntax:
162
163 default private;
164
165 All variables and functions are by default private.
166
167 default private variables public functions;
168
169 All variables are by default private, but functions are public.
170
Zesstra50c62672017-06-20 22:04:51 +0200171 Only the modifiers 'private', 'protected', 'visible' and 'public'
172 (and 'static' for functions only) are allowed here.
MG Mud User88f12472016-06-24 23:31:02 +0200173
174 The default visibility thus set affects only variables/functions with
Zesstra7ea4a032019-11-26 20:11:40 +0100175 no explicit visibility:
MG Mud User88f12472016-06-24 23:31:02 +0200176
177 default private;
178
179 int private_var;
180 public int public_var;
181
182 The definition is valid from the point of the 'default' statement
183 until the end of the file, or until the next 'default' statement:
184
185 default private;
186
187 int private_var;
188
189 default public;
190
191 int public_var;
192
193 Note that this default visibility does not affect inherits.
194
195
196HISTORY
197 The modifier 'static' for variables was renamed to 'nosave'
198 with LDMud 3.2.8. 'static' is still recognized as an alias.
199
200 The default visibility was added in LDMud 3.2.9 as experimental
201 feature.
202
Zesstra50c62672017-06-20 22:04:51 +0200203 LDMud 3.5 introduced 'visible' as a name for the default
204 visibility.
205
MG Mud User88f12472016-06-24 23:31:02 +0200206SEE ALSO
207 closures(LPC), inheritance(LPC), functions(LPC), types(LPC)