-
Notifications
You must be signed in to change notification settings - Fork 42
/
enum.vroom
68 lines (44 loc) · 1.78 KB
/
enum.vroom
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
As always, we have to start by installing maktaba:
:set nocompatible
:let g:maktabadir = fnamemodify($VROOMFILE, ':p:h:h')
:let g:bootstrapfile = g:maktabadir . '/bootstrap.vim'
:execute 'source' g:bootstrapfile
Vroom provides simple enum types.
:let g:animals = maktaba#enum#Create(['CAT', 'DOG', 'DUCK', 'PIG'])
By default, they are ordered by index, starting at zero.
:echomsg g:animals.DUCK
~ 2
You may also create an enum from a dictionary with custom values.
:let g:birds = maktaba#enum#Create({'DUCK': 'Yes', 'PENGUIN': 'Definitely.'})
:echomsg g:birds.DUCK
~ Yes
All names must be uppercase.
:let g:newenum = maktaba#function#Create('maktaba#enum#Create')
:call maktaba#error#Try(g:newenum.WithArgs(['trombone']))
~ ERROR(BadValue): Invalid enum name: trombone. Enum names must be uppercase.
Enums come with methods to get at their names, sorted by the natural order of
their values,
:echomsg string(g:birds.Names())
~ ['PENGUIN', 'DUCK']
their values (sorted by natural order):
:echomsg string(g:animals.Values())
~ [0, 1, 2, 3]
individual names:
:echomsg g:animals.Name(0)
~ CAT
and individual values:
:echomsg g:animals.Value('DOG')
~ 1
Note that these last two functions throw NotFound errors on failure.
:let g:namer = maktaba#function#Method(g:animals, 'Name')
:call maktaba#error#Try(g:namer.WithArgs(10))
~ ERROR(NotFound): Enum name with value 10.
:let g:valuer = maktaba#function#Method(g:animals, 'Value')
:call maktaba#error#Try(g:valuer.WithArgs('WHEEL'))
~ ERROR(NotFound): Enum name WHEEL.
Using .Value is otherwise equivalent to accessing the name field directly.
:echomsg g:animals.DOG
~ 1
Enums may not be empty.
:call maktaba#error#Try(g:newenum.WithArgs([]))
~ ERROR(BadValue): Enum must have at least one name.