OSDN Git Service

【更新内容】
[ring-lang-081/ring.git] / docs / en / source / typehints.txt
1 .. index:: 
2         single: The Type Hints Library; Introduction to the Type Hints Library
3
4 ======================================
5 Introduction to the Type Hints Library
6 ======================================
7
8 In this chapter we will learn about the Type Hints Library
9
10
11 .. index:: 
12         pair: The Type Hints Library; Why Type Hints?
13
14 Why Type Hints?
15 ===============
16
17 Using this library we can add the type information to the source code which will be 
18 very useful for tools like
19
20 * Code Editors 
21 * Static-Analysis
22
23 .. note:: Ring is a dynamic language, No type checking will be done by the compiler.
24
25 .. index:: 
26         pair: The Type Hints Library; Example
27
28 Example
29 =======
30
31 The next example will use the Type Hints library
32
33 .. code-block:: ring
34
35         load "typehints.ring"
36
37         see sum(3,4) + nl ;
38         see sayHello("Mahmoud");
39
40         int func sum(int x,int y) {
41                 return x+y ;
42         }
43
44         string func sayHello(string name) {
45                 return "Hello " + name ;
46         }
47
48 .. index:: 
49         pair: The Type Hints Library; User Types
50
51 User Types
52 ==========
53
54 The Type Hints library is very powerful and will support user types (Classes) automatically
55
56 Example:
57
58 .. code-block:: ring
59
60         load "typehints.ring"
61
62         import mypackage 
63
64         test()  { main([:one,:two,:three]) }
65
66         myclass func test() {
67                 see "Testing User Types!" + nl
68                 return new myclass
69         }
70
71         package mypackage {
72                 public class myclass {
73                         public static void func main(list args) {
74                                 see "welcome" + nl
75                                 see args
76                         }
77                 }
78         }
79
80 .. index:: 
81         pair: The Type Hints Library; Using Types inside Code
82
83 Using Types inside Code
84 =======================
85
86 Also you can use the types inside the code (not only the function prototype)
87
88 Example:
89
90 .. code-block:: ring
91
92         load "typehints.ring"
93
94         int     sum = sum(3,4)
95         string  msg = sayHello("Mahmoud")
96
97         see "Sum = " + sum + nl + msg + nl
98
99
100         int func sum(int x,int y) {
101                 return x+y ;
102         }
103
104         string func sayHello(string name) {
105                 return "Hello " + name ;
106         }
107
108 .. index:: 
109         pair: The Type Hints Library; Rules
110
111 Rules
112 =====
113
114 * To use the types in the function prototype you must use '(' and ')' around parameters
115 * To use the types in the function code, You must set the variable value (Assignment).
116
117 The next types are defined by the library
118
119 .. code-block:: ring
120
121         # Low Level Types
122         char             
123         unsigned         
124         signed           
125         int              
126         short            
127         long             
128         float            
129         double           
130         void             
131
132         # High Level Types 
133         string           
134         list             
135         number           
136         object           
137
138         # Other
139         public           
140         static           
141         abstract         
142         protected        
143         override         
144
145