Types in Scol » History » Version 1
iri, 09/24/2012 10:01 PM
1 | 1 | iri | h1. Types in Scol |
---|---|---|---|
2 | |||
3 | The most used Scol types are : |
||
4 | |||
5 | I : integer |
||
6 | S : string |
||
7 | F : float |
||
8 | Chn : SCOL channel |
||
9 | Srv : SCOL server |
||
10 | Env : SCOL environment |
||
11 | Comm : communication (typically, between VMs) |
||
12 | [u0 u1] : Tuples where u0, u1 are Scol types |
||
13 | [u0 rn] : List where u0 is a Scol type, rn is the level of recursivity (r1, in the mostly cases) |
||
14 | |||
15 | All these types are availables from the minimal environment. |
||
16 | |||
17 | However, you can create yourself a type for your application. It will be available only from your application (unless you share it in a library by example). |
||
18 | |||
19 | For that, you are two different ways : *struct* or *typedef*. Their uses are differents, of course. |
||
20 | |||
21 | h2. struct |
||
22 | |||
23 | struct creates a structure like the C-structure (almost ...). A structure is a type with several fields. A name and a Scol type define each field. In fact, each field is an internal and opaque function. |
||
24 | The first letter of the structure name must be an uppercase letter. There is no constraint for the fields. |
||
25 | |||
26 | <pre> |
||
27 | struct <Name_of_structure> = [<name_field_1> <type1>, <name_field_2> <type2>, ..., <name_field_N> <typeN>]] mk<Name_of_structure>;; |
||
28 | </pre> |
||
29 | |||
30 | Like _typeof_ or _var_, the declaration ends by a double semicolon. |
||
31 | All names are choosen by you, at your convenience, except _struct_, of course. |
||
32 | mk<Name_of_structure> is the constructor for your structure. It is also a function (fun [<type1> <type2> ... <typeN>] <Name_of_structure>). You must *initialize* (build) your structure after its declaration and before its use ! |
||
33 | |||
34 | h3. Example : |
||
35 | |||
36 | <pre> |
||
37 | // Declaration |
||
38 | struct MYDATA [ MD_sName : S, MD_iValue : I ] mkMYDATA;; |
||
39 | |||
40 | // Initialization (build) |
||
41 | fun initData (name, value)= |
||
42 | mkMYDATA [name value] |
||
43 | ;; |
||
44 | |||
45 | // Get a value (here, the field 'name') |
||
46 | fun getDataName (mydatastr)= |
||
47 | mydatastr.MD_sName |
||
48 | ;; |
||
49 | |||
50 | // Set a value |
||
51 | fun setDataName (mydatastr, newname)= |
||
52 | set mydatastr.MD_sName = newname |
||
53 | ;; |
||
54 | |||
55 | fun main ()= |
||
56 | // first, you initialize the structure ! |
||
57 | let initData "Bob" 1 -> mydatastr in |
||
58 | ( |
||
59 | // you display a value |
||
60 | _fooS strcat ">>> name : " (getDataName mydatastr); |
||
61 | // you change a field |
||
62 | setDataName mydatastr "Alice"; |
||
63 | // you display a value |
||
64 | _fooS strcat ">>> name : " (getDataName mydatastr); |
||
65 | 0 |
||
66 | ); |
||
67 | ;; |
||
68 | </pre> |
||
69 | |||
70 | This allows to create an encapsulated object, opaque or not. Another example can be found in [[A console in Scol]], with a mini-api around a structure. Structures are common objects in Scol. |
||
71 | |||
72 | h2. typedef |
||
73 | |||
74 | The use is more complex. |
||
75 | It allows to have several possible types for a same variable or any other valid object. This is an equivalent at the C-union. This is normally forbidden in Scol (the type of a variable is strong, fixed and not modifiable). |
||
76 | For each different type inside the new type, an internal and opaque function, named constructor, translates the value object. |
||
77 | |||
78 | To define it : |
||
79 | |||
80 | <pre> |
||
81 | typedef <new_type> = <constructor_1> <type_1> | <constructor_2> <type_2> | ... | <constructor_N> <type_N> | <constructor_default> ;; |
||
82 | </pre> |
||
83 | |||
84 | <constructor_X> is a function, its type is fun [ <type_X> ] <new_type> |
||
85 | |||
86 | The names of a constructor and of a new type are free. |
||
87 | <type_X> must be a known type in the current environment : either a Scol type or any customized type (via struct or typedef) declared previously. |
||
88 | |||
89 | The last constructor has never a <type_X>. This is the default constructor; it is used if no match with other constructors. |
||
90 | |||
91 | h3. Example : |
||
92 | |||
93 | <pre> |
||
94 | /* New scol type named 'NewType'. It can set by a "subtype" (in fact in function) NTI, NTF, ... */ |
||
95 | typedef NewType = |
||
96 | NTI I |
||
97 | | NTF F |
||
98 | | NTS S |
||
99 | | NTn;; |
||
100 | </pre> |
||
101 | |||
102 | For you, if the value is an integer, NTI will be asked. If it is an float, it will be NTF, a string with NTS, otherwise by NTn. |
||
103 | |||
104 | After be defined, you can use it ! |
||
105 | A such object is processed using the *match* function : |
||
106 | |||
107 | <pre> |
||
108 | fun anyFunction ()= |
||
109 | match <variable> with |
||
110 | (<constructor_1> <local_variable_1> -> instruction) |
||
111 | | (<constructor_2> <local_variable_2> -> instruction) |
||
112 | ... |
||
113 | | (<constructor_default> -> instruction); |
||
114 | 0;; |
||
115 | </pre> |
||
116 | |||
117 | You can write less of constructors than defined in the type declaration. Others cases will be considered by the default constructor. |
||
118 | |||
119 | | (<constructor_default> -> instruction); |
||
120 | is the same thing that |
||
121 | | (_ -> instruction); |
||
122 | |||
123 | So, we get : |
||
124 | |||
125 | <pre> |
||
126 | typedef NewType = |
||
127 | NTI I |
||
128 | | NTF F |
||
129 | | NTS S |
||
130 | | NTn;; |
||
131 | |||
132 | typeof X = NewType;; /* global ariable NewType */ |
||
133 | |||
134 | fun myFunction ()= |
||
135 | match X with |
||
136 | (NTI e -> (_fooS "X is an integer"; 0)) |
||
137 | | (NTF e -> (_fooS "X is a float"; 0)) |
||
138 | | (NTS e -> (_fooS "X is a string"; 0)) |
||
139 | | (_ -> (_fooS "X is another thing"; 0));; |
||
140 | |||
141 | fun main ()= |
||
142 | _showconsole; |
||
143 | |||
144 | set X = NTI 10; |
||
145 | myFunction; |
||
146 | set X = NTF 5.0; |
||
147 | myFunction; |
||
148 | set X = NTS "Scol is a free language"; |
||
149 | myFunction; |
||
150 | |||
151 | 0;; |
||
152 | </pre> |
||
153 | |||
154 | |||
155 | |||
156 | License : "CC-BY-SA-2.0":https://creativecommons.org/licenses/by-sa/2.0/ |
||
157 | Tutorial by iri |
||
158 | Updated by / |