-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial.lua
More file actions
145 lines (111 loc) · 2.9 KB
/
tutorial.lua
File metadata and controls
145 lines (111 loc) · 2.9 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
-- Tutorial for EmmyDoc
-- https://emmylua.github.io
-- https://github.com/EmmyLua
--------------------------------------------------------------------------------
-- Tutorial for annotation "@class" and "@field"
---define a class
---**Markdown** is available for comment strings
---@class Person comment string goes here
---@field public age number age value
---@field weight number
local Person = {}
function Person:sayHello()
print('Hi!')
end
---define class Emmy
---@class Emmy : Person @`Emmy` inherited from `Person`
local EmmyClass = {
name = "Emmy",
["*love daddy*"] = true
}
function EmmyClass:sayHello()
print('Hi! I am Emmy.')
end
---@param book string
function EmmyClass:reading(book)
end
---define another class Daddy
---@class Daddy : Person
local DaddyClass = {
name = 'Tang'
}
---@param code string
function DaddyClass:coding(code)
end
--------------------------------------------------------------------------------
-- Tutorial for annotation "@type"
---@type Emmy
local inst1
-- type "inst1." here
---@type Emmy[]
local array = {}
local inst2 = array[1]
-- type "inst2." here
for i, emmy in ipairs(array) do
-- type "emmy." here
end
---@type table<string, Emmy>
local dict = {}
local inst3 = dict['test']
-- type "inst3." here
for k, emmy in pairs(dict) do
-- type "emmy." here
end
---@type fun(name:string):Emmy
local creatorFn
local inst4 = creatorFn('inst4')
-- type "inst4." here
--------------------------------------------------------------------------------
-- Tutorial for annotation "@return"
---@return Emmy
local function createEmmy()
end
local inst = createEmmy()
-- type "inst." here
-- inst.
--------------------------------------------------------------------------------
-- Tutorial for annotation "@param"
---@param emmy Emmy
local function testHello(emmy)
-- type "emmy:" here
end
local list = {}
---@param emmy Emmy
for i, emmy in ipairs(list) do
-- type "emmy:" here
end
---@param emmy Emmy
pcall(function(emmy)
-- type "emmy:" here
end, inst)
--------------------------------------------------------------------------------
-- Tutorial for @generic
---@generic T : Person
---@param clazz T
---@return T
local function createPersonInstance(clazz)
--type "clazz:" here
-- todo return a instance of clazz
end
local emmy = createPersonInstance(EmmyClass)
-- type "emmy:" here
local daddy = createPersonInstance(DaddyClass)
-- type "daddy:" here
-- one more complex generic test
---@generic T : Person
---@param clazz T
---@param initHandler fun(person:T):void
---@return T
local function createPersonInstance2(clazz, initHandler)
local instance = clazz() -- new clazz()
initHandler(instance)
return instance
end
local emmy2 = createPersonInstance2(EmmyClass, function(emmy)
-- type "emmy:" here
end)
-- type "emmy2:" here
local daddy2 = createPersonInstance2(DaddyClass, function(daddy)
-- type "daddy:" here
end)
-- type "daddy2:" here