comparison runtime/doc/vim9class.txt @ 33025:1d18c7fe609f v9.0.1804

patch 9.0.1804: Vim9: no support for private object methods Commit: https://github.com/vim/vim/commit/cd7293bf6c358bb0e183582a2927fc03566d29f6 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Sun Aug 27 19:18:23 2023 +0200 patch 9.0.1804: Vim9: no support for private object methods Problem: Vim9: no support for private object methods Solution: Add support for private object/class methods closes: #12920 Signed-off-by: Christian Brabandt <cb@256bit.org> Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
author Christian Brabandt <cb@256bit.org>
date Sun, 27 Aug 2023 19:30:05 +0200
parents d5c05e15cf81
children d42927c6e556
comparison
equal deleted inserted replaced
33024:e5b8fc37e882 33025:1d18c7fe609f
176 endif 176 endif
177 return this._lnum 177 return this._lnum
178 enddef 178 enddef
179 179
180 180
181 Private methods ~
182 If you want object methods to be accessible only from other methods of the
183 same class and not used from outside the class, then you can make them
184 private. This is done by prefixing the method name with an underscore: >
185
186 class SomeClass
187 def _Foo(): number
188 return 10
189 enddef
190 def Bar(): number
191 return this._Foo()
192 enddef
193 endclass
194 <
195 Accessing a private method outside the class will result in an error (using
196 the above class): >
197
198 var a = SomeClass.new()
199 a._Foo()
200 <
181 Simplifying the new() method ~ 201 Simplifying the new() method ~
182 202
183 Many constructors take values for the object members. Thus you very often see 203 Many constructors take values for the object members. Thus you very often see
184 this pattern: > 204 this pattern: >
185 205
281 enddef 301 enddef
282 endclass 302 endclass
283 303
284 Inside the class the function can be called by name directly, outside the 304 Inside the class the function can be called by name directly, outside the
285 class the class name must be prefixed: `OtherThing.ClearTotalSize()`. 305 class the class name must be prefixed: `OtherThing.ClearTotalSize()`.
306
307 Just like object methods the access can be made private by using an underscore
308 as the first character in the method name: >
309
310 class OtherThing
311 static def _Foo()
312 echo "Foo"
313 enddef
314 def Bar()
315 OtherThing._Foo()
316 enddef
317 endclass
286 318
287 ============================================================================== 319 ==============================================================================
288 320
289 4. Using an abstract class *Vim9-abstract-class* 321 4. Using an abstract class *Vim9-abstract-class*
290 322