💾 Archived View for gmi.noulin.net › vim › vim9class.gmi captured on 2024-08-19 at 03:52:34. Gemini links have been rewritten to link to archived content
View Raw
More Information
⬅️ Previous capture (2024-05-12)
-=-=-=-=-=-=-
- vim9class.txt* For Vim version 9.1. Last change: 2024 Apr 13
VIM REFERENCE MANUAL by Bram Moolenaar
Vim9 classes, objects, interfaces, types and enums. *vim9-class*
1. Overview |Vim9-class-overview|
2. A simple class |Vim9-simple-class|
3. Class variables and methods |Vim9-class-member|
4. Using an abstract class |Vim9-abstract-class|
5. Using an interface |Vim9-using-interface|
6. More class details |Vim9-class|
7. Type definition |Vim9-type|
8. Enum |Vim9-enum|
9. Rationale
10. To be done later
==============================================================================
1. Overview *Vim9-class-overview*
The fancy term is "object-oriented programming". You can find lots of study
material on this subject. Here we document what |Vim9| script provides,
assuming you know the basics already. Added are helpful hints about how to
use this functionality effectively. Vim9 classes and objects cannot be used
in legacy Vim scripts and legacy functions.
The basic item is an object:
- An object stores state. It contains one or more variables that can each
have a value.
- An object provides functions that use and manipulate its state. These
functions are invoked "on the object", which is what sets it apart from the
traditional separation of data and code that manipulates the data.
- An object has a well defined interface, with typed member variables and
methods.
- Objects are created from a class and all objects have the same interface.
This does not change at runtime, it is not dynamic.
An object can only be created by a class. A class provides:
- A new() method, the constructor, which returns an object for the class.
This method is invoked on the class name: MyClass.new().
- State shared by all objects of the class: class variables (class members).
- A hierarchy of classes, with super-classes and sub-classes, inheritance.
An interface is used to specify properties of an object:
- An object can declare several interfaces that it implements.
- Different objects implementing the same interface can be used the same way.
The class hierarchy allows for single inheritance. Otherwise interfaces are
to be used where needed.
Class modeling ~
You can model classes any way you like. Keep in mind what you are building,
don't try to model the real world. This can be confusing, especially because
teachers use real-world objects to explain class relations and you might think
your model should therefore reflect the real world. It doesn't! The model
should match your purpose.
Keep in mind that composition (an object contains other objects) is often
better than inheritance (an object extends another object). Don't waste time
trying to find the optimal class model. Or waste time discussing whether a
square is a rectangle or that a rectangle is a square. It doesn't matter.
==============================================================================
2. A simple class *Vim9-simple-class*
Let's start with a simple example: a class that stores a text position (see
below for how to do this more efficiently): >
class TextPosition
var lnum: number
var col: number
def new(lnum: number, col: number)
this.lnum = lnum
this.col = col
enddef
def SetLnum(lnum: number)
this.lnum = lnum
enddef
def SetCol(col: number)
this.col = col
enddef
def SetPosition(lnum: number, col: number)
this.lnum = lnum
this.col = col
enddef
endclass
< *object* *Object*
You can create an object from this class with the new() method: >
var pos = TextPosition.new(1, 1)
<
The object variables "lnum" and "col" can be accessed directly: >
echo