Skip to content

Commit

Permalink
patch 9.0.1001: classes are not documented or implemented yet
Browse files Browse the repository at this point in the history
Problem:    Classes are not documented or implemented yet.
Solution:   Make the first steps at documenting Vim9 objects, classes and
            interfaces.  Make initial choices for the syntax.  Add a skeleton
            implementation.  Add "public" and "this" in the command table.
  • Loading branch information
brammool committed Dec 4, 2022
1 parent b21b8e9 commit c1c365c
Show file tree
Hide file tree
Showing 18 changed files with 907 additions and 106 deletions.
2 changes: 2 additions & 0 deletions Filelist
Expand Up @@ -163,6 +163,7 @@ SRC_ALL = \
src/version.h \
src/vim.h \
src/vim9.h \
src/vim9class.c \
src/vim9cmds.c \
src/vim9compile.c \
src/vim9execute.c \
Expand Down Expand Up @@ -327,6 +328,7 @@ SRC_ALL = \
src/proto/usercmd.pro \
src/proto/userfunc.pro \
src/proto/version.pro \
src/proto/vim9class.pro \
src/proto/vim9cmds.pro \
src/proto/vim9compile.pro \
src/proto/vim9execute.pro \
Expand Down
2 changes: 2 additions & 0 deletions runtime/doc/Makefile
Expand Up @@ -161,6 +161,7 @@ DOCS = \
version9.txt \
vi_diff.txt \
vim9.txt \
vim9class.txt \
visual.txt \
windows.txt \
workshop.txt
Expand Down Expand Up @@ -313,6 +314,7 @@ HTMLS = \
vi_diff.html \
vimindex.html \
vim9.html \
vim9class.html \
visual.html \
windows.html \
workshop.html
Expand Down
93 changes: 12 additions & 81 deletions runtime/doc/vim9.txt
Expand Up @@ -16,7 +16,7 @@ features in Vim9 script.
3. New style functions |fast-functions|
4. Types |vim9-types|
5. Namespace, Import and Export |vim9script|
6. Future work: classes |vim9-classes|
6. Classes and interfaces |vim9-classes|

9. Rationale |vim9-rationale|

Expand Down Expand Up @@ -1940,73 +1940,17 @@ Or: >
==============================================================================

6. Future work: classes *vim9-classes*

Above "class" was mentioned a few times, but it has not been implemented yet.
Most of Vim9 script can be created without this functionality, and since
implementing classes is going to be a lot of work, it is left for the future.
For now we'll just make sure classes can be added later.

Thoughts:
- `class` / `endclass`, the whole class must be in one file
- Class names are always CamelCase (to avoid a name clash with builtin types)
- A single constructor called "constructor" (similar to TypeScript)
- Single inheritance: `class ThisClass extends BaseClass`
- `interface` / `endinterface` (looks like a class without any implementation)
- Explicit declaration that the class supports an interface, so that type
checking works properly:
`class SomeClass implements SomeInterface, OtherInterface`
- `abstract class` (class with incomplete implementation) - not really needed?
- Class (static) methods and Object methods: syntax to be defined.
- Class (static) members and Object members: syntax to be defined.
- Access control: private / protected / shared / public ? Keep it simple.
- Access object members with `this.member` ?
- Generics for class: `class <Tkey, Tentry>`
- Generics for function: `def <Tkey> GetLast(key: Tkey)`
- Method overloading (two methods with the same name but different argument
types): Most likely not
- Mixins: not sure if that is useful, leave out for simplicity.

Again, much of this is from TypeScript with a slightly different syntax.

Some things that look like good additions:
- Use a class as an interface (like Dart)
- Extend a class with methods, using an import (like Dart)
- Mixins
- For testing: Mock mechanism

An important class that will be provided is "Promise". Since Vim is single
threaded, connecting asynchronous operations is a natural way of allowing
plugins to do their work without blocking the user. It's a uniform way to
invoke callbacks and handle timeouts and errors.

Some commands have already been reserved:
*:class*
*:endclass*
*:abstract*
*:enum*
*:endenum*
*:interface*
*:endinterface*
*:static*
*:type*

Some examples: >
abstract class Person
static const prefix = 'xxx'
var name: string
def constructor(name: string)
this.name = name
enddef
def display(): void
echo name
enddef
abstract def find(string): Person
endclass
6. Classes and interfaces *vim9-classes*

In legacy script a Dictionary could be used as a kind-of object, by adding
members that are functions. However, this is quite inefficient and requires
the writer to do the work of making sure all the objects have the right
members. See |Dictionary-function|.

In |Vim9| script you can have classes, objects and interfaces like in most
popular object-oriented programming languages. Since this is a lot of
functionality it is located in a separate help file: |vim9class.txt|.


==============================================================================

Expand Down Expand Up @@ -2293,18 +2237,5 @@ tool need to be supported. Since most languages support classes the lack of
support for classes in Vim is then a problem.


Classes ~

Vim supports a kind-of object oriented programming by adding methods to a
dictionary. With some care this can be made to work, but it does not look
like real classes. On top of that, it's quite slow, because of the use of
dictionaries.

It would be good to support real classes, and this is planned for a later
version. The support is a "minimal common functionality" of class support in
most languages. It will work much like Java, which is the most popular
programming language.



vim:tw=78:ts=8:noet:ft=help:norl:

0 comments on commit c1c365c

Please sign in to comment.