A very simple example using actors
[nit.git] / lib / actors / examples / simple / simple.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # A very simple example of the actor model
16 module simple
17
18 import actors
19
20
21 # A class anotated with `actor`
22 # It automatically gets the `async` property to make asynchronous calls on it
23 class A
24 actor
25
26 # Prints "foo"
27 fun foo do print "foo"
28
29 # Returns i^2
30 fun bar(i : Int): Int do return i * i
31 end
32
33 # Create a new instance of A
34 var a = new A
35
36 # Make an asynchronous call
37 a.async.foo
38
39 # Make a synchronous call
40 a.foo
41
42 # Make an asynchronous call
43 # Which return a `Future[Int]` instead of `Int`
44 var r = a.async.bar(5)
45
46 # Retrieve the value of the future
47 print r.join
48
49 # Make a Synchronous call
50 print a.bar(5)