|
1 |
| -*vim9class.txt* For Vim version 9.1. Last change: 2024 Mar 03 |
| 1 | +*vim9class.txt* For Vim version 9.1. Last change: 2024 Mar 28 |
2 | 2 |
|
3 | 3 |
|
4 | 4 | VIM REFERENCE MANUAL by Bram Moolenaar
|
@@ -904,19 +904,125 @@ aliased: >
|
904 | 904 |
|
905 | 905 | 8. Enum *Vim9-enum* *:enum* *:endenum*
|
906 | 906 |
|
907 |
| -{not implemented yet} |
908 |
| - |
| 907 | + *enum* *E1418* *E1419* *E1420* |
909 | 908 | An enum is a type that can have one of a list of values. Example: >
|
910 | 909 |
|
911 |
| - :enum Color |
912 |
| - White |
913 |
| - Red |
914 |
| - Green |
915 |
| - Blue |
916 |
| - Black |
917 |
| - :endenum |
| 910 | + :enum Color |
| 911 | + White, |
| 912 | + Red, |
| 913 | + Green, Blue, Black |
| 914 | + :endenum |
| 915 | +< |
| 916 | + *enumvalue* *E1422* |
| 917 | +The enum values are separated by commas. More than one enum value can be |
| 918 | +listed in a single line. The final enum value should not be followed by a |
| 919 | +comma. |
| 920 | + |
| 921 | +An enum value is accessed using the enum name followed by the value name: > |
| 922 | +
|
| 923 | + var a: Color = Color.Blue |
| 924 | +< |
| 925 | +Enums are treated as classes, where each enum value is essentially an instance |
| 926 | +of that class. Unlike typical object instantiation with the |new()| method, |
| 927 | +enum instances cannot be created this way. |
| 928 | + |
| 929 | +An enum can only be defined in a |Vim9| script file. *E1414* |
| 930 | +An enum cannot be defined inside a function. |
| 931 | + |
| 932 | + *E1415* |
| 933 | +An enum name must start with an uppercase letter. The name of an enum value |
| 934 | +in an enum can start with an upper or lowercase letter. |
| 935 | + |
| 936 | + *E1416* |
| 937 | +An enum can implement an interface but cannot extend a class: > |
| 938 | +
|
| 939 | + enum MyEnum implements MyIntf |
| 940 | + Value1, |
| 941 | + Value2 |
| 942 | +
|
| 943 | + def SomeMethod() |
| 944 | + enddef |
| 945 | + endenum |
| 946 | +< |
| 947 | + *enum-constructor* |
| 948 | +The enum value objects in an enum are constructed like any other objects using |
| 949 | +the |new()| method. Arguments can be passed to the enum constructor by |
| 950 | +specifying them after the enum value name, just like calling a function. The |
| 951 | +default constructor doesn't have any arguments. |
| 952 | + |
| 953 | + *E1417* |
| 954 | +An enum can contain class variables, class methods, object variables and |
| 955 | +object methods. The methods in an enum cannot be |:abstract| methods. |
| 956 | + |
| 957 | +The following example shows an enum with object variables and methods: > |
| 958 | +
|
| 959 | + vim9script |
| 960 | + enum Planet |
| 961 | + Earth(1, false), |
| 962 | + Jupiter(95, true), |
| 963 | + Saturn(146, true) |
| 964 | +
|
| 965 | + var moons: number |
| 966 | + var has_rings: bool |
| 967 | + def GetMoons(): number |
| 968 | + return this.moons |
| 969 | + enddef |
| 970 | + endenum |
| 971 | + echo Planet.Jupiter.GetMoons() |
| 972 | + echo Planet.Earth.has_rings |
| 973 | +< |
| 974 | + *E1421* *E1423* *E1424* *E1425* |
| 975 | +Enums and their values are immutable. They cannot be modified after |
| 976 | +declaration and cannot be utilized as numerical or string types. |
| 977 | + |
| 978 | + *enum-name* |
| 979 | +Each enum value object has a "name" instance variable which contains the name |
| 980 | +of the enum value. This is a readonly variable. |
| 981 | + |
| 982 | + *enum-ordinal* *E1426* |
| 983 | +Each enum value has an associated ordinal number starting with 0. The ordinal |
| 984 | +number of an enum value can be accessed using the "ordinal" instance variable. |
| 985 | +This is a readonly variable. Note that if the ordering of the enum values in |
| 986 | +an enum is changed, then their ordinal values will also change. |
| 987 | + |
| 988 | + *enum-values* |
| 989 | +All the values in an enum can be accessed using the "values" class variable |
| 990 | +which is a List of the enum objects. This is a readonly variable. |
| 991 | + |
| 992 | +Example: > |
| 993 | + enum Planet |
| 994 | + Mercury, |
| 995 | + Venus, |
| 996 | + Earth |
| 997 | + endenum |
| 998 | +
|
| 999 | + echo Planet.Mercury |
| 1000 | + echo Planet.Venus.name |
| 1001 | + echo Planet.Venus.ordinal |
| 1002 | + for p in Planet.values |
| 1003 | + # ... |
| 1004 | + endfor |
| 1005 | +< |
| 1006 | +An enum is a class with class variables for the enum value objects and object |
| 1007 | +variables for the enum value name and the enum value ordinal: > |
| 1008 | +
|
| 1009 | + enum Planet |
| 1010 | + Mercury, |
| 1011 | + Venus |
| 1012 | + endenum |
| 1013 | +< |
| 1014 | +The above enum definition is equivalent to the following class definition: > |
| 1015 | +
|
| 1016 | + class Planet |
| 1017 | + public static final Mercury: Planet = Planet.new('Mercury', 0) |
| 1018 | + public static final Venus: Planet = Planet.new('Venus', 1) |
918 | 1019 |
|
| 1020 | + public static const values: list<Planet> = [Planet.Mercury, Planet.Venus] |
919 | 1021 |
|
| 1022 | + public const name: string |
| 1023 | + public const ordinal: number |
| 1024 | + endclass |
| 1025 | +< |
920 | 1026 | ==============================================================================
|
921 | 1027 |
|
922 | 1028 | 9. Rationale
|
|
0 commit comments