summaryrefslogtreecommitdiffstats
path: root/runtime
diff options
context:
space:
mode:
authorYegappan Lakshmanan <yegappan@yahoo.com>2023-08-27 19:18:23 +0200
committerChristian Brabandt <cb@256bit.org>2023-08-27 19:18:23 +0200
commitcd7293bf6c358bb0e183582a2927fc03566d29f6 (patch)
treed1fe17d7fdd8f5a2414761eab2879a80494e4e27 /runtime
parent03e44a1d70e914504e6151fe88ad1e574cbf0a59 (diff)
patch 9.0.1804: Vim9: no support for private object methodsv9.0.1804
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>
Diffstat (limited to 'runtime')
-rw-r--r--runtime/doc/todo.txt1
-rw-r--r--runtime/doc/vim9class.txt32
2 files changed, 33 insertions, 0 deletions
diff --git a/runtime/doc/todo.txt b/runtime/doc/todo.txt
index 8479e15d3c..6b45aabde7 100644
--- a/runtime/doc/todo.txt
+++ b/runtime/doc/todo.txt
@@ -129,6 +129,7 @@ Further Vim9 improvements, possibly after launch:
or: def _Func()
Perhaps use "private" keyword instead of "_" prefix?
- "final" object members - can only be set in the constructor.
+ - Support export/import of classes and interfaces.
- Cannot use class type of itself in the method (Issue #12369)
- Cannot use an object method in a lambda #12417
Define all methods before compiling them?
diff --git a/runtime/doc/vim9class.txt b/runtime/doc/vim9class.txt
index c68288a0c5..926638bad5 100644
--- a/runtime/doc/vim9class.txt
+++ b/runtime/doc/vim9class.txt
@@ -178,6 +178,26 @@ number to the total number of lines: >
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 @@ object members, they cannot use the "this" keyword. >
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*