-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo.sol
213 lines (142 loc) · 3.16 KB
/
demo.sol
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
-----------------------------------------------------
--[[
# SOL IS:
* Lua + static typing
* Flexible, plausible typing
# SOL IS NOT:
* Waterproof
* More than Lua (yet!)
--]]
-----------------------------------------------------
-- Undefined globals
--global x = 1337
local a = x
-----------------------------------------------------
-- Argument checking
local Foo = {}
Foo.member()
Foo:member()
Foo.member(Foo)
Foo:member(Foo)
-- Note - no forward-declare needed!
function Foo:member()
self.member()
self:member()
self.member(self)
self:member(self)
end
-----------------------------------------------------
-- pairs/ipairs
do
local list = { "one", "two", "three" }
for i,v in pairs(list) do
print(i .. ': ' .. v)
end
local map = { ['one'] = 1, ['two'] = 2, ['three'] = 3 }
for k,v in ipairs(map) do
k = k + '42'
v = v + 42
end
end
-----------------------------------------------------
-- Add a type annotation to the function
local function int_to_string(arg)
return '' .. arg
end
local function string_to_int(arg)
if arg then
return tonumber(arg)
else
return 0
end
end
var i = int_to_string("42")
i = i + 42
local function cmp(a: int?, b: string?) -> bool?
return a == b
--return true
--return nil
end
typedef fmt_string = string -- TODO
local function foo(fmt: fmt_string, ...)
end
foo("hello", 1, 2, 3, 3.14)
-----------------------------------------------------
-- What is the return type of this?
local function win_or_fail()
if math.random() < 0.5 then
return true
else
return false, "bad luck"
end
end
local win, err_msg, too_many = win_or_fail()
-----------------------------------------------------
-- var vs local
local local_can_be_anything = require 'unfindable'
var var_must_be_deducible = some_lua_function()
var var_can_be_explicit = (require 'unfindable') : int
-----------------------------------------------------
-- Maps and lists:
do
var<[int]> list = {1, 2, 3}
list[1] = 42
list[2] = '1337'
typedef Int2str = {int => string}
var map = {} : Int2str
map[1] = 'one'
map[2] = 'two'
map['three'] = 3
end
-----------------------------------------------------
-- Enums
typedef Tribool = 'yes' or 'no' or 'maybe'
var ok = 'yes' : Tribool
var bad = 'wrong' : Tribool
local function do_stuff(how: "quickly" or "slowly")
end
do_stuff("quickly")
do_stuff("sakta")
-----------------------------------------------------
-- Function pointers
typedef IntFilter = function(int) -> bool
local function filter_number(fp: IntFilter, doWhat: function(int))
for i=1,42 do
if fp(i) then
doWhat(i)
end
end
end
filter_number(
function(i: int) return i%2==1 end,
function(i: int) print(i .. " is odd") end
)
-----------------------------------------------------
-- Objects and inheritence
typedef Node = {
tag: 'Foo' or 'Bar';
korv: int;
}
typedef FooNode : Node = {
tag: 'Foo';
int: int;
wild: int;
}
typedef BarNode : Node = {
tag: 'Bar';
string: string;
wild: string;
}
local function work_on_node(n: Node)
if n.tag == 'Foo' then
var f = n : FooNode
end
var<[string]> a = n.wild
end
work_on_node({
tag = 'Foo';
korv = 32
})
work_on_node({
tag = 'Baz'
})