A very simple example using actors
authorBlackMinou <romain.chanoir@viacesi.fr>
Wed, 1 Feb 2017 19:11:15 +0000 (14:11 -0500)
committerBlackMinou <romain.chanoir@viacesi.fr>
Mon, 27 Feb 2017 15:06:12 +0000 (10:06 -0500)
Signed-off-by: BlackMinou <romain.chanoir@viacesi.fr>

lib/actors/examples/simple/makefile [new file with mode: 0644]
lib/actors/examples/simple/simple.nit [new file with mode: 0644]
tests/sav/simple.res [new file with mode: 0644]

diff --git a/lib/actors/examples/simple/makefile b/lib/actors/examples/simple/makefile
new file mode 100644 (file)
index 0000000..9699264
--- /dev/null
@@ -0,0 +1,11 @@
+default: compile
+
+compile:
+       nitc simple.nit
+
+run:
+       ./simple
+
+clean:
+       rm actors_simple.nit
+       rm simple
diff --git a/lib/actors/examples/simple/simple.nit b/lib/actors/examples/simple/simple.nit
new file mode 100644 (file)
index 0000000..8c98b40
--- /dev/null
@@ -0,0 +1,50 @@
+# This file is part of NIT (http://www.nitlanguage.org).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# A very simple example of the actor model
+module simple
+
+import actors
+
+
+# A class anotated with `actor`
+# It automatically gets the `async` property to make asynchronous calls on it
+class A
+       actor
+
+       # Prints "foo"
+       fun foo do print "foo"
+
+       # Returns i^2
+       fun bar(i : Int): Int do return i * i
+end
+
+# Create a new instance of A
+var a = new A
+
+# Make an asynchronous call
+a.async.foo
+
+# Make a synchronous call
+a.foo
+
+# Make an asynchronous call
+# Which return a `Future[Int]` instead of `Int`
+var r = a.async.bar(5)
+
+# Retrieve the value of the future
+print r.join
+
+# Make a Synchronous call
+print a.bar(5)
diff --git a/tests/sav/simple.res b/tests/sav/simple.res
new file mode 100644 (file)
index 0000000..2b10d5a
--- /dev/null
@@ -0,0 +1,4 @@
+foo
+foo
+25
+25