-
Notifications
You must be signed in to change notification settings - Fork 437
Expand file tree
/
Copy pathbasicStructures.re
More file actions
506 lines (397 loc) · 12.3 KB
/
Copy pathbasicStructures.re
File metadata and controls
506 lines (397 loc) · 12.3 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/* Copyright (c) 2015-present, Facebook, Inc. All rights reserved. */
let run = fun () => {TestUtils.printSection "Basic Structures";};
while something {
print_string ("You're in a while loop");
print_newline ();
};
for i in 0 to 5 {
print_int (i);
print_newline ();
for i in 10 downto 0 {
print_string ("Counting in reverse direction");
print_newline ();
};
};
for i in 0 to (endOfRangeMustBeSimple expr soWrap) {
print_int (i);
print_newline ();
for i in (theSame isTrue ofThe startOfRange) downto 0 {
print_string ("Counting in reverse direction");
print_newline ();
};
};
let x = !foo.bar;
let x = !foo..bar;
let x = !(!foo).bar;
let x = !(!foo)..bar;
/* Prefix operator */
let x = !!foo.bar;
let x = !!foo..bar;
let x = !~foo.bar;
let x = !~foo..bar;
let noParensNeeded = !blah.foo.bar;
let parensNeededAroundFirst = (!blah).foo.bar;
let parensNeededAroundSecond = (!blah.foo).bar;
let noParensNeeded = !blah..foo..bar;
let parensNeededAroundFirst = (!blah)..foo..bar;
let parensNeededAroundSecond = (!blah..foo)..bar;
/* The following two have an error in formatting! We must check for
* *consecutive* prefix operators and either space separate or guard in
* parens. */
let x = !(!foo.bar);
let x = !(!foo..bar);
/* Comments */
/*Below is an empty comment*/
/**/
/** IF
*============================================================================
*/
let something = if self.ext.logSuccess {
print_string "Did tap";
print_newline ();
};
let logTapSuccess = fun self => if self.ext.logSuccess {
print_string "Did tap";
print_newline ();
} else {
();
};
let logTapSuccess self => if self.ext.logSuccess {
print_string "Did tap";
print_newline ();
};
(!data).field = true;
(!data).field1.field2 = true;
(!data.field1).field2 = true;
((!data).field1).field2 = true;
(!(data.field1)).field2 = true;
let loop appTime frameTime => {
if hasSetup.contents {
setupScene ();
renderIntoTop ();
hasSetup.contents = true;
};
process appTime frameTime;
};
/* These parens should be kept around the entire last if/then/else */
if something {
if somethingElse {
();
} else {
"blah";
};
};
/* These parens should be kept around just the last if/then*/
if something {if somethingElse {();} else {"blah";};};
/* Parens should be generated to wrap the entire final if then else.
* To test that it's being parsed correclty, should print "one". */
if true {
if true {
print_string "one";
} else {
print_string "two";
};
};
/* Should print two */
if true {
if false {
print_string "one";
} else {
print_string "two";
};
};
/* Should not print */
if false {
if true {
print_string "one";
} else {
print_string "two";
};
};
/* Should wrap (if a > b then a else b).
* printer(
*/
let printIfFirstArgGreater = true;
let result =
if printIfFirstArgGreater {
fun a b => if (a > b) {print_string "a > b";} else {print_string "b >= a";};
} else if {
fun a b => if (a > b) {print_string "b < a";} else {print_string "a <= b";};
} {
print_string ("That could never possibly type check");
print_newline ();
};
let myRecord = {
nestedRecord: {
anotherNestedRecord: fun instaComp displayRect =>
if (Graphics.cgRectIntersectsWithSlop
defaultCompositeTimerRectSlop instaComp.relativeRect displayRect) {
IoEligible;
} else {
IoInelibleButTryComposition;
}
}
};
if printIfFirstArgGreater {
fun a b => if (a > b) {print_string "a > b";};
} else {fun a b => if (a > b) {print_string "b < a";};};
/* Should Be Parsed As: Cleary a type error, but at least the parsing makes that clear */
if printIfFirstArgGreater {
fun a b =>
if (a > b) {
print_string "a > b";
} else {
fun a b => if (a > b) {print_string "b < a";};
};
};
fun a b => if (a > b) {print_string "a > b";};
/* What you probably wanted was: */
if printIfFirstArgGreater {
fun a b => (if (a > b) {print_string "a > b";});
} else {
fun a b => (if (a > b) {print_string "b < a";});
};
/* Mutative if statement: Not used to evaluate to something. */
if (10 < 100) {
let msg = "If there was any doubt, 10 is in fact less than 100.";
print_string (msg);
} else {
let msg = "All bets are off.";
print_string (msg);
};
if (10 < 100) {
print_string ("If there was any doubt, 10 is in fact less than 100.");
} else {
print_string ("All bets are off.");
};
/** TYPE CONSTRAINTS
*============================================================================
*/
let x = (10:int);
let x:int = 10;
let (x:int) = 10;
let (x:int) = (10:int);
/* let (x:int) = (10:string); */
/* let (x:string) = ("hello":int); */
/** TUPLES
*============================================================================
*/
/* In Reason, types look like the data they model! Tuples are no exception. */
type pairOfInts = (int, int);
let (letBindingWithTypeConstraint:int) = 10;
let ((tupleItem:int), (withTypeConstraint:int)) = (10, 20);
/* To make sure that tuple field annotations are annotating the entire field */
let _dummyFunc x => 10;
let annotatingFuncApplication = (_dummyFunc "a":int, _dummyFunc "a":int);
/* Pretty printer might stick the [int] at the label. */
let annotatingSingleFuncApplication = (_dummyFunc "a":int);
/* So lets try a place where it won't */
let annotatingSingleFuncApplication = {
/* Commenting a let binding. */
let a = 100;
/* Commenting another let binding. */
let int = 200;
/*
* This demonstrates why named arguments cannot simply have the form (func
* arg:val) - it is indistinguishable from a type constraint.
*/
2 + (_dummyFunc a:int);
};
let (tupleItem:int, constrainedWithoutGrouping:int) = (10, 20);
let (tupleItem, withOutsideTypeConstraint):(int,int) = (10, 20);
/** Immutable Lists
* ============================================================================
*/
/* Anatomy: -Head- --------- Tail--------- nil: You can't see nil */
let x: list int = [ 1, 2, 3, 4, 5, 6, 7, 8, 9];
let hd = "appendedToHead";
let tl = ["listTo", "append", "to"];
/* To push *one* and only *one* item to the front of a list - use [hd, ...tl] */
let result: list string = [hd, ...tl];
/* Is the same as writing */
let result: list string = ["appendedToHead", "listTo", "append", "to"];
/* To operate on lists, use pattern matching */
let rec size = fun
| [] => 0
| [hd, ...tl] => 1 + (size tl);
/* Optimize for tail recursion */
let rec size = fun soFar lst => switch lst {
| [] => 0
| [hd, ...tl] => (size (soFar + 1) tl)
};
let nestedMatch lstLst => switch lstLst {
| [hd, ...tl] when false => 10
| [hd, ...tl] => switch tl {
| [] => 0 + 0
| [tlHd, ...tlTl] => 0 + 1
}
| [] => 0
};
let nestedMatchWithWhen lstLst => switch lstLst {
| [hd, ...tl] when false => 10
| [hd, ...tl] when true => switch tl {
| [] when false => 0 + 0
| [] when true => 0 + 0
| [tlHd, ...tlTl] => 0 + 1
}
| [] => 0
};
/**
* Aliasing with "as" during matches.
*/
type mine = MyThing int | YourThing int;
/*
* Reason parses "as" aliases differently than OCaml.
*/
let ppp = switch (MyThing 20) {
| MyThing x as ppp
| YourThing x as ppp => ppp
};
let MyThing _ as ppp | YourThing _ as ppp = ppp;
/*
* in order to achieve the previous example in ocaml, you would have to group
* as:
*/
let ppp = switch (MyThing 20) {
| (MyThing x as ppp)
| (YourThing x as ppp) => ppp
};
let (MyThing _ as ppp) |(YourThing _ as ppp) = ppp;
/*
* But this isn't needed in Reason because OR patterns have much lower
* precedence - they should be pretty printed in the same way.
*/
/* TODO: */
/* let rec nestedMatch lstLst => match lstLst with { */
/* hd::tl: match tl with { */
/* []: 0 + 0, */
/* tlHd::tlTl: 0 + 1, */
/* }, */
/* []: 0 */
/* }; */
/* */
/** ARRAYS
* ============================================================================
* Arrays are weird looking. Usually you want lists because they support pattern
* matching - that's why they have nicer syntax - to entice you. But if you want
* random access and better control over memory layout, use arrays.
*/
let emptyArray = [||];
let arrayWithOne = [|10|];
let arrayWithTwo = [|10, 10|];
let secondItem = Array.get arrayWithTwo 1;
/* Getting And Setting: Yeah, we should really change this */
/* Get an array item at index 1 */
let secondItem = arrayWithTwo.(1);
/* Set an array item at index 1 */
arrayWithTwo.(1) = 300;
/**
* STRINGS
* ============================================================================
* The language supports mutating strings, but that should not be depended upon.
*/
let myString = "asdf";
myString.[2] = '9'; /* Replacing a character: I could do without this sugar */
/* FUNCTIONS
*=============================================================================
*/
/* TYPE ANNOTATIONS
* =============================================================================
*/
let one = 900;
let two = 10000;
/* Tuple expressions can be annotated without additional paren wrapping */
let myTuple = (one:int, two:int);
type myTupleType = (int, int);
let myTuple = (myTuple:myTupleType);
/* Anything *outside* of a tuple, must still be annotated within parens. */
let myTuple = ((one:int, two:int):myTupleType);
/* Now functions that accept a single argument being a tuple look familiar */
let addValues = fun (a:int, b:int) => {
a + b;
};
let addValues = fun (a:int, b:int) => {
a + b;
};
let myFunction = fun (a : int) (b : int) : int => a + b;
let functionReturnValueType (i:int, s:string): (int => int) => fun x => x + 1;
let curriedFormOne (i:int, s:string) => s ^ (string_of_int i);
let curriedFormTwo (i:int, x:int) :(int, int) => (i, x);
/* let nonCurriedFormTwo = fun (i:int, x:int) (:(int, int)) => (i, x); */
let curriedFormThree (i:int, (a:int, b:int):(int, int)) :(int, int, int) => (i, a, b);
/* let nonCurriedFormThree = fun (i:int, (a:int, b:int):(int, int)) (:(int, int, int)) => (i, a, b); */
/** TODO: But this, however doesn't work.
* let (myCurriedFunc: int => int) a => a;
* Note: This is likely because only "simple patterns" are accepted as constraints
* in let bindings - that may be easy to change.
*/
type myFuncType = (int, int) => int;
let myFunc: myFuncType = fun (a,b) => a + b;
let funcWithTypeLocallyAbstractTypes (type atype) (type btype) a b (c: atype => btype => unit) => c a b;
/**
* Records:
*=============================================================================
*/
type withThreeFields = {
name: string,
age: int,
occupation: string
};
let testRecord = {
name: "joe",
age: 20,
occupation: "engineer"
};
let anotherRecord = {
...testRecord,
name: "joe++",
age: testRecord.age + 10
};
let makeRecordBase () => {name: "Joe", age: 30, occupation: "Engineer"};
let anotherRecord = {
/* These parens should be evaporated. */
...(makeRecordBase ()),
name: "joe++",
age: testRecord.age + 10
};
let anotherRecord = {
/* Comments should be correctly placed before ... expression */
...makeRecordBase (),
/* Comment after record extension */
name: "joe++",
age: testRecord.age + 10
};
let anotherRecord = {
/* Currently, type annotations must be wrapped in parens - that's easy to improve */
...(makeRecordBase () : withThreeFields),
name: "joe++",
age: testRecord.age + 10
};
let anotherRecord = {
/* This is meaningless, sure */
...someArray.[0] = 20,
name: "joe++",
age: testRecord.age + 10
};
let anotherRecord = {
...SomeReally.longFunctionCall {
passingRecordField: 0,
andThisOtherRecordField: 10
},
name: "joe++",
age: testRecord.age + 10
};
let anotherRecord = {
...SomeReally.longFunctionCall
withArguments
(thatWrap:bool),
name: "joe++",
age: testRecord.age + 10
};
let anotherRecord = {
...SomeReally.longFunctionCall
withArg
["and", "final", "list", "that", "should", "break"],
name: "joe++",
age: testRecord.age + 10
};