blob: 163ed1c26b0ea2fe54bb57e38d75c232292dd6b9 [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 ~~~~~~~~~~~~
Zesstra3085c662025-08-02 18:31:10 +0200110 It is also possible to redeclare all variables, functions and/or
111 structs of an inherited object for the own object at the inheriting
MG Mud User88f12472016-06-24 23:31:02 +0200112 statement:
113
114 private functions nosave variables inherit "complex/room";
115 public variables inherit "complex/room";
116 private functions inherit "complex/room";
Zesstra3085c662025-08-02 18:31:10 +0200117 private structs inherit "complex/room";
MG Mud User88f12472016-06-24 23:31:02 +0200118
Zesstra3085c662025-08-02 18:31:10 +0200119 To redeclare a function, variable or struct declared public in the
MG Mud User88f12472016-06-24 23:31:02 +0200120 inherited object to be private or protected is not possible.
121
Zesstra50c62672017-06-20 22:04:51 +0200122 The following table shows the result of the combination of
123 visibility modifiers at the function declaration and inherit
124 statement:
125
126 Function | Inheritance modifiers
127 modifier | public | visible | static | protected | private
128 -----------+-----------+-----------+-----------+-----------+---------
129 public | public | public | public | public | public
130 visible | public | visible | static | protected | private
131 static | static | static | static | protected | private
132 protected | protected | protected | protected | protected | private
133 private | private | private | private | private | private
134
MG Mud User88f12472016-06-24 23:31:02 +0200135 There also exists a modifier explicitly for the inherit statement:
136
137 virtual -- inherits the given object virtually. This only makes
138 sense in a complex inherit tree.
139 If an object is inherited normally (not virtually)
140 twice somewhere in the inherit tree the intern
141 variables exist twice. If inherited virtually they
142 exist only once.
143 Example:
144 A inherits B and C.
145 B inherits D.
146 C inherits D.
147 If the inheritance of D is virtual in B and C
148 D's variables exist only once in A. If A changes
149 D's variables via functions of B this also changes
150 the variables of D as known by C.
151
152 virtual: non-virtual:
153 A A
154 / \ / \
155 B C B C
156 \ / | |
157 D D D
158
159
160 To simplify the adoption of existing code, LPC allows to specify
Zesstra3085c662025-08-02 18:31:10 +0200161 a default visibility for functions, variables and structs, using
162 a syntax similar to the inherit syntax:
MG Mud User88f12472016-06-24 23:31:02 +0200163
164 default private;
165
Zesstra3085c662025-08-02 18:31:10 +0200166 All variables, functions and structs are by default private.
MG Mud User88f12472016-06-24 23:31:02 +0200167
Zesstra3085c662025-08-02 18:31:10 +0200168 default private variables public functions public structs;
MG Mud User88f12472016-06-24 23:31:02 +0200169
Zesstra3085c662025-08-02 18:31:10 +0200170 All variables are by default private, but functions and
171 structs are public.
MG Mud User88f12472016-06-24 23:31:02 +0200172
Zesstra50c62672017-06-20 22:04:51 +0200173 Only the modifiers 'private', 'protected', 'visible' and 'public'
174 (and 'static' for functions only) are allowed here.
MG Mud User88f12472016-06-24 23:31:02 +0200175
Zesstra3085c662025-08-02 18:31:10 +0200176 The default visibility thus set affects only functions/variables/
177 structs with no explicit visibility:
MG Mud User88f12472016-06-24 23:31:02 +0200178
179 default private;
180
181 int private_var;
182 public int public_var;
183
184 The definition is valid from the point of the 'default' statement
185 until the end of the file, or until the next 'default' statement:
186
187 default private;
188
189 int private_var;
190
191 default public;
192
193 int public_var;
194
195 Note that this default visibility does not affect inherits.
196
197
198HISTORY
199 The modifier 'static' for variables was renamed to 'nosave'
200 with LDMud 3.2.8. 'static' is still recognized as an alias.
201
202 The default visibility was added in LDMud 3.2.9 as experimental
203 feature.
204
Zesstra50c62672017-06-20 22:04:51 +0200205 LDMud 3.5 introduced 'visible' as a name for the default
206 visibility.
207
Zesstra3085c662025-08-02 18:31:10 +0200208 LDMud 3.6.6 introduced inheritance and default modifiers for structs.
209
MG Mud User88f12472016-06-24 23:31:02 +0200210SEE ALSO
211 closures(LPC), inheritance(LPC), functions(LPC), types(LPC)