diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pair.rhope	Tue Apr 28 23:06:07 2009 +0000
@@ -0,0 +1,42 @@
+/*
+	This example program defines a new object type Pair with two properties and one worker
+*/
+
+//We're importing extendlib for access to Pretty Print
+Import extendlib.rhope
+
+//Blueprint statements define new object types
+Blueprint Pair
+{
+	//We define two fields here First and Second
+	First
+	Second
+}
+
+//There's nothing special about worker's named New. It's just a convention.
+New@Pair[first,second:out]
+{
+	//The Build worker creates a new unpopulated object of a given type based on its blueprint
+	out <- [[Build["Pair"]
+	//Here we set the properties of the new object, First and Second, to the values passed to the constructor
+	]First <<[first]
+	]Second <<[second]
+}
+
+//Rhope has no concept of operators; everything is a worker. Here we define a worker for adding two Pairs
++@Pair[left,right:sum]
+{
+	//Get the sum of the two fields
+	first <- [[left]First >>] + [[right]First >>]
+	second <- [[left]Second >>] + [[right]Second >>]
+	//Return a modified Pair
+	sum <-	[[left]First <<[first]]Second <<[second]
+}
+
+Main[]
+{
+	a <- New@Pair[2,3]
+	b <- New@Pair[5,-1]
+	//Pretty Print can print out user defined objects and most builtin objects in a readable format
+	Pretty Print[[a]+[b], ""]
+}