Project

General

Profile

Types in Scol » History » Version 3

lpousse, 07/07/2017 10:32 AM

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 2 iri
tab u0  : Array
15 1 iri
16
All these types are availables from the minimal environment.
17
18
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).
19
20
For that, you are two different ways : *struct* or *typedef*. Their uses are differents, of course.
21
22
h2. struct
23
24
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.
25
The first letter of the structure name must be an uppercase letter. There is no constraint for the fields.
26
27
<pre>
28
struct <Name_of_structure> = [<name_field_1> <type1>, <name_field_2> <type2>, ..., <name_field_N> <typeN>]] mk<Name_of_structure>;;
29
</pre>
30
31
Like _typeof_ or _var_, the declaration ends by a double semicolon.
32
All names are choosen by you, at your convenience, except _struct_, of course.
33
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 !
34
35
h3. Example :
36
37
<pre>
38
// Declaration
39 3 lpousse
struct MYDATA = [ MD_sName : S, MD_iValue : I ] mkMYDATA;;
40 1 iri
41
// Initialization (build)
42
fun initData (name, value)=
43
  	mkMYDATA [name value]
44
;;
45
46
// Get a value (here, the field 'name')
47
fun getDataName (mydatastr)=
48
  	mydatastr.MD_sName
49
;;
50
51
// Set a value
52
fun setDataName (mydatastr, newname)=
53
	set mydatastr.MD_sName = newname
54
;;
55
56
fun main ()=
57
	// first, you initialize the structure !
58
  	let initData "Bob" 1 -> mydatastr in
59
    (
60
    // you display a value
61
    _fooS strcat ">>> name : " (getDataName mydatastr);
62
    // you change a field
63
    setDataName mydatastr "Alice";
64
    // you display a value
65
    _fooS strcat ">>> name : " (getDataName mydatastr);
66
 	0
67
 	);
68
;;
69
</pre>
70
71
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.
72
73
h2. typedef
74
75
The use is more complex.
76
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).
77
For each different type inside the new type, an internal and opaque function, named constructor, translates the value object.
78
79
To define it :
80
81
<pre>
82
typedef <new_type> = <constructor_1> <type_1> | <constructor_2> <type_2> | ... | <constructor_N> <type_N> | <constructor_default> ;;
83
</pre>
84
85
<constructor_X> is a function, its type is fun [ <type_X> ] <new_type>
86
87
The names of a constructor and of a new type are free.
88
<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.
89
90
The last constructor has never a <type_X>. This is the default constructor; it is used if no match with other constructors.
91
92
h3. Example :
93
94
<pre>
95
/* New scol type named 'NewType'. It can set by a "subtype" (in fact in function) NTI, NTF, ... */
96
typedef NewType =    
97
NTI I
98
| NTF F
99
| NTS S
100
| NTn;;
101
</pre>
102
103
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.
104
105
After be defined, you can use it !
106
A such object is processed using the *match* function :
107
108
<pre>
109
fun anyFunction ()=
110
	match <variable> with
111
	(<constructor_1> <local_variable_1> -> instruction)
112
	| (<constructor_2> <local_variable_2> -> instruction)
113
	...
114
	| (<constructor_default> -> instruction);
115
	0;;
116
</pre>
117
	
118
You can write less of constructors than defined in the type declaration. Others cases will be considered by the default constructor.
119
120
| (<constructor_default> -> instruction);
121
is the same thing that
122
| (_ -> instruction);
123
124
So, we get :
125
126
<pre>
127
typedef NewType =    
128
NTI I
129
| NTF F
130
| NTS S
131
| NTn;;
132
133
typeof X = NewType;;    /* global ariable NewType */
134
135
fun myFunction ()=
136
    match X with
137
    (NTI e -> (_fooS "X is an integer"; 0))
138
    | (NTF e -> (_fooS "X is a float"; 0))
139
    | (NTS e -> (_fooS "X is a string"; 0))
140
    | (_ -> (_fooS "X is another thing"; 0));;
141
    
142
fun main ()=
143
	_showconsole;
144
	
145
	set X = NTI 10;
146
	myFunction;
147
	set X = NTF 5.0;
148
	myFunction;
149
	set X = NTS "Scol is a free language";
150
	myFunction;
151
	
152
	0;;
153
</pre>
154
	
155
	
156
157
License : "CC-BY-SA-2.0":https://creativecommons.org/licenses/by-sa/2.0/
158
Tutorial by iri
159
Updated by /