Mercurial > repos > rhope
comparison pair.rhope @ 0:76568becd6d6
Rhope Alpha 2a source import
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Tue, 28 Apr 2009 23:06:07 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:76568becd6d6 |
---|---|
1 /* | |
2 This example program defines a new object type Pair with two properties and one worker | |
3 */ | |
4 | |
5 //We're importing extendlib for access to Pretty Print | |
6 Import extendlib.rhope | |
7 | |
8 //Blueprint statements define new object types | |
9 Blueprint Pair | |
10 { | |
11 //We define two fields here First and Second | |
12 First | |
13 Second | |
14 } | |
15 | |
16 //There's nothing special about worker's named New. It's just a convention. | |
17 New@Pair[first,second:out] | |
18 { | |
19 //The Build worker creates a new unpopulated object of a given type based on its blueprint | |
20 out <- [[Build["Pair"] | |
21 //Here we set the properties of the new object, First and Second, to the values passed to the constructor | |
22 ]First <<[first] | |
23 ]Second <<[second] | |
24 } | |
25 | |
26 //Rhope has no concept of operators; everything is a worker. Here we define a worker for adding two Pairs | |
27 +@Pair[left,right:sum] | |
28 { | |
29 //Get the sum of the two fields | |
30 first <- [[left]First >>] + [[right]First >>] | |
31 second <- [[left]Second >>] + [[right]Second >>] | |
32 //Return a modified Pair | |
33 sum <- [[left]First <<[first]]Second <<[second] | |
34 } | |
35 | |
36 Main[] | |
37 { | |
38 a <- New@Pair[2,3] | |
39 b <- New@Pair[5,-1] | |
40 //Pretty Print can print out user defined objects and most builtin objects in a readable format | |
41 Pretty Print[[a]+[b], ""] | |
42 } |