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>
diff --git a/runtime/doc/vim9class.txt b/runtime/doc/vim9class.txt
index c68288a..926638b 100644
--- a/runtime/doc/vim9class.txt
+++ b/runtime/doc/vim9class.txt
@@ -178,6 +178,26 @@
enddef
+Private methods ~
+If you want object methods to be accessible only from other methods of the
+same class and not used from outside the class, then you can make them
+private. This is done by prefixing the method name with an underscore: >
+
+ class SomeClass
+ def _Foo(): number
+ return 10
+ enddef
+ def Bar(): number
+ return this._Foo()
+ enddef
+ endclass
+<
+Accessing a private method outside the class will result in an error (using
+the above class): >
+
+ var a = SomeClass.new()
+ a._Foo()
+<
Simplifying the new() method ~
Many constructors take values for the object members. Thus you very often see
@@ -284,6 +304,18 @@
Inside the class the function can be called by name directly, outside the
class the class name must be prefixed: `OtherThing.ClearTotalSize()`.
+Just like object methods the access can be made private by using an underscore
+as the first character in the method name: >
+
+ class OtherThing
+ static def _Foo()
+ echo "Foo"
+ enddef
+ def Bar()
+ OtherThing._Foo()
+ enddef
+ endclass
+
==============================================================================
4. Using an abstract class *Vim9-abstract-class*