Merge: doc: fixed some typos and other misc. corrections
[nit.git] / examples / extern_methods.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012-2013 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Sample program using extern methods implemented in C and other FFI services
18 module extern_methods
19
20 redef enum Int
21 # Get the `self`th Fibonacci number
22 #
23 # The FFI can be used to optimize performance critical methods.
24 # Although this implementation is recursive, so there is not much gain.
25 # Notice the `import fib` telling the compiler that there is a callback to `foo` from the C code.
26 # From C, `foo` is called through the function `Int_fib` with the receiver as argument.
27 fun fib: Int import fib `{
28 if (self < 2)
29 return self;
30 else
31 return Int_fib(self-1) + Int_fib(self-2);
32 `}
33
34 # System call to sleep for `self` seconds
35 #
36 # You can use the FFI to access any system functions, sometimes it's extremely simple.
37 fun sleep_seconds `{
38 sleep(self);
39 `}
40
41 # Print the result of adding `self` to `self.fib` with callbacks from C
42 #
43 # This method illustrates many forms of callbacks from C,
44 # but it would have been better implemented in Nit (as done by `bar`).
45 #
46 # It is recommended to avoid callbacks when possible, use each language according to their strengths.
47 # Nit is better than C to call methods and manipulate objects.
48 # C has easy access to system functions, native libraries and it does low-level stuff.
49 #
50 # The implementation calls:
51 # * the local `fib` method,
52 # * the `+` operator, a method of `Int`,
53 # * `to_s`, a method of all objects,
54 # * `String.to_cstring`, a method of `String` to get as a `char*`.
55 fun foo import fib, +, to_s, String.to_cstring `{
56 long self_fib = Int_fib(self);
57 long self_plus_fib = Int__plus(self, self_fib);
58
59 String nit_string = Int_to_s(self_plus_fib);
60 char *c_string = String_to_cstring(nit_string);
61
62 printf("from C: self + fib(self) = %s\n", c_string);
63 `}
64
65 # Equivalent to `foo` but written in pure Nit
66 fun bar do print "from Nit: self + fib(self) = {self+self.fib}"
67 end
68
69 redef class Float
70 # Arctangent of `self` and `other` using `atan2` from `math.h`
71 #
72 # Universal types (Int, Float, Bool, etc.) are converted to their closest equivalent in C
73 # (long, double, int, etc.).
74 fun atan_with(other: Float): Float `{
75 return atan2(self, other);
76 `}
77 end
78
79 print 12.fib
80
81 print "sleeping 1 second..."
82 1.sleep
83
84 print 100.0.atan_with(200.0)
85 8.foo
86 8.bar