ae25e68a45a4560b89d228dbcdbabeed904e0582
[scilab.git] / scilab / modules / functions / help / pt_BR / overloading.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!--
3  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4  * Copyright (C) INRIA
5  *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14  *
15  -->
16 <refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" xml:id="overloading" xml:lang="pt">
17     <refnamediv>
18         <refname>overloading</refname>
19         <refpurpose>capacidades de overloading ("sobrecarga") de exibições,
20             funções e operadores
21         </refpurpose>
22     </refnamediv>
23     <refsection>
24         <title>Descrição</title>
25         <para>No Scilab, exibições, funções e operadores de variáveis podem ser
26             definidos para novos objetos utilizando funções (codificadas no Scilab ou
27             primitivas).
28         </para>
29         <variablelist>
30             <varlistentry>
31                 <term>Exibição (Display)</term>
32                 <listitem>
33                     <para>a exibição de objetos definidos por uma estrutura
34                         <literal>tlist</literal> pode ser sobrecarregada (a exibição padrão
35                         é semelhante a de <literal>list</literal>'s). A função de sobrecarga
36                         não deve ter argumentos de saída e deve ter um único argumento de
37                         entrada. Seu nome é formado como segue:
38                         <literal>%&lt;tlist_type&gt;_p</literal> onde
39                         <literal>%&lt;tlist_type&gt;</literal> significa a primeira entrada
40                         do componente do tipo <literal>tlist</literal> truncado aos 9
41                         primeiros caracteres.
42                     </para>
43                 </listitem>
44             </varlistentry>
45             <varlistentry>
46                 <term>Operadores (Operators)</term>
47                 <listitem>
48                     <para>cada operador que não está definido para dados tipos de
49                         operandos pode ser definido. A função de sobrecarga deve ter um
50                         único argumento de saída e um ou dois de entrada de acordo com o
51                         número de operandos. O nome da função é definido como segue:
52                     </para>
53                 </listitem>
54             </varlistentry>
55         </variablelist>
56         <para>para operadores binários:
57             <literal>%&lt;tipo_do_primeiro_operando&gt;_&lt;código_do_
58                 operador&gt;_&lt;tipo_do_segundo_operando&gt;
59             </literal>
60         </para>
61         <para>para operadores unários:
62             <literal>%&lt;tipo_do_operando&gt;_&lt;código_do_operador&gt;</literal>
63         </para>
64         <para>Operadores de extração e inserção que são n-nários são descritos
65             abaixo.
66         </para>
67         <warning>
68             Be careful, only the types registered by the
69             <function>typename</function> function can be used in an
70             overloading macros.
71         </warning>
72         <para>
73             <literal>&lt;tipo_do_operando&gt;</literal>,
74             <literal>&lt;tipo_do_primeiro_operando&gt;</literal>,
75             <literal>&lt;tipo_do_segundo_operando&gt;</literal> são seqüências de
76             caracteres associadas a cada tipo de dado como descrito na tabela
77             seguinte:
78         </para>
79         <informaltable border="1">
80             <tr>
81                 <td>tipo de dado</td>
82                 <td>código "char"</td>
83                 <td>tipo de dado</td>
84                 <td>código "char"</td>
85             </tr>
86             <tr>
87                 <td>constante</td>
88                 <td>
89                     <literal>s</literal>
90                 </td>
91                 <td>booleano</td>
92                 <td>
93                     <literal>b</literal>
94                 </td>
95             </tr>
96             <tr>
97                 <td>string</td>
98                 <td>
99                     <literal>c</literal>
100                 </td>
101                 <td>bibilioteca</td>
102                 <td>
103                     <literal>f</literal>
104                 </td>
105             </tr>
106             <tr>
107                 <td>ponteiro de função</td>
108                 <td>
109                     <literal>fptr</literal>
110                 </td>
111                 <td>manipulador</td>
112                 <td>
113                     <literal>h</literal>
114                 </td>
115             </tr>
116             <tr>
117                 <td>inteiro</td>
118                 <td>
119                     <literal>i</literal>
120                 </td>
121                 <td>lista</td>
122                 <td>
123                     <literal>l</literal>
124                 </td>
125             </tr>
126             <tr>
127                 <td>função</td>
128                 <td>
129                     <literal>m</literal>
130                 </td>
131                 <td>função compilada</td>
132                 <td>
133                     <literal>mc</literal>
134                 </td>
135             </tr>
136             <tr>
137                 <td>polinômio</td>
138                 <td>
139                     <literal>p</literal>
140                 </td>
141                 <td>esparso</td>
142                 <td>
143                     <literal>sp</literal>
144                 </td>
145             </tr>
146             <tr>
147                 <td>esparso booleano</td>
148                 <td>
149                     <literal>spb</literal>
150                 </td>
151                 <td>tlist</td>
152                 <td>
153                     <literal>tlist_type</literal>
154                 </td>
155             </tr>
156             <tr>
157                 <td>lista implícita</td>
158                 <td>
159                     <literal>ip</literal>
160                 </td>
161                 <td>matriz esparsa do Matlab</td>
162                 <td>
163                     <literal>msp</literal>
164                 </td>
165             </tr>
166             <tr>
167                 <td>mlist</td>
168                 <td>
169                     <literal>mlist_type</literal>
170                 </td>
171                 <td>ponteiro</td>
172                 <td>
173                     <literal>ptr</literal>
174                 </td>
175             </tr>
176         </informaltable>
177         <para>
178             <literal>&lt;código_do_operador&gt;</literal> é um único caractere
179             associado a cada operador como descrito na tabela seguinte:
180         </para>
181         <informaltable border="1">
182             <tr>
183                 <td>
184                     <literal>op</literal>
185                 </td>
186                 <td>char code</td>
187             </tr>
188             <tr>
189                 <td>
190                     <literal>'</literal>
191                 </td>
192                 <td>t</td>
193             </tr>
194             <tr>
195                 <td>
196                     <literal>+</literal>
197                 </td>
198                 <td>a</td>
199             </tr>
200             <tr>
201                 <td>
202                     <literal>-</literal>
203                 </td>
204                 <td>s</td>
205             </tr>
206             <tr>
207                 <td>
208                     <literal>*</literal>
209                 </td>
210                 <td>m</td>
211             </tr>
212             <tr>
213                 <td>
214                     <literal>/</literal>
215                 </td>
216                 <td>r</td>
217             </tr>
218             <tr>
219                 <td>
220                     <literal>\</literal>
221                 </td>
222                 <td>l</td>
223             </tr>
224             <tr>
225                 <td>
226                     <literal>^</literal>
227                 </td>
228                 <td>p</td>
229             </tr>
230             <tr>
231                 <td>
232                     <literal>.*</literal>
233                 </td>
234                 <td>x</td>
235             </tr>
236             <tr>
237                 <td>
238                     <literal>./</literal>
239                 </td>
240                 <td>d</td>
241             </tr>
242             <tr>
243                 <td>
244                     <literal>.\</literal>
245                 </td>
246                 <td>q</td>
247             </tr>
248             <tr>
249                 <td>
250                     <literal>.*.</literal>
251                 </td>
252                 <td>k</td>
253             </tr>
254             <tr>
255                 <td>
256                     <literal>./.</literal>
257                 </td>
258                 <td>y</td>
259             </tr>
260             <tr>
261                 <td>
262                     <literal>.\.</literal>
263                 </td>
264                 <td>z</td>
265             </tr>
266             <tr>
267                 <td>
268                     <literal>:</literal>
269                 </td>
270                 <td>b</td>
271             </tr>
272             <tr>
273                 <td>
274                     <literal>*.</literal>
275                 </td>
276                 <td>u</td>
277             </tr>
278             <tr>
279                 <td>
280                     <literal>/.</literal>
281                 </td>
282                 <td>v</td>
283             </tr>
284             <tr>
285                 <td>
286                     <literal>\.</literal>
287                 </td>
288                 <td>w</td>
289             </tr>
290             <tr>
291                 <td>
292                     <literal>[a,b]</literal>
293                 </td>
294                 <td>c</td>
295             </tr>
296             <tr>
297                 <td>
298                     <literal>[a;b]</literal>
299                 </td>
300                 <td>f</td>
301             </tr>
302             <tr>
303                 <td>
304                     <literal>() extraction</literal>
305                 </td>
306                 <td>e</td>
307             </tr>
308             <tr>
309                 <td>
310                     <literal>() insertion</literal>
311                 </td>
312                 <td>i</td>
313             </tr>
314             <tr>
315                 <td>
316                     <literal>==</literal>
317                 </td>
318                 <td>o</td>
319             </tr>
320             <tr>
321                 <td>
322                     <literal>&lt;&gt;</literal>
323                 </td>
324                 <td>n</td>
325             </tr>
326             <tr>
327                 <td>
328                     <literal>|</literal>
329                 </td>
330                 <td>g</td>
331             </tr>
332             <tr>
333                 <td>
334                     <literal>&amp;</literal>
335                 </td>
336                 <td>h</td>
337             </tr>
338             <tr>
339                 <td>
340                     <literal>.^</literal>
341                 </td>
342                 <td>j</td>
343             </tr>
344             <tr>
345                 <td>
346                     <literal>.'</literal>
347                 </td>
348                 <td>0</td>
349             </tr>
350             <tr>
351                 <td>
352                     <literal>&lt;</literal>
353                 </td>
354                 <td>1</td>
355             </tr>
356             <tr>
357                 <td>
358                     <literal>&gt;</literal>
359                 </td>
360                 <td>2</td>
361             </tr>
362             <tr>
363                 <td>
364                     <literal>&lt;=</literal>
365                 </td>
366                 <td>3</td>
367             </tr>
368             <tr>
369                 <td>
370                     <literal>&gt;=</literal>
371                 </td>
372                 <td>4</td>
373             </tr>
374             <tr>
375                 <td>
376                     <literal>~</literal>
377                 </td>
378                 <td>5</td>
379             </tr>
380             <tr>
381                 <td>
382                     <literal>iext</literal>
383                 </td>
384                 <td>6</td>
385             </tr>
386         </informaltable>
387         <para>A função de sobrecarga para sintaxe de extração
388             <literal>b=a(i1,...,in)</literal> tem a seguinte seqüência de chamamento:
389             <literal>b=%&lt;tipo_de_a&gt;_e_(i1,...,in,a)</literal>
390         </para>
391         <para>
392             e a sintaxe<literal>[x1,..,xm]=a(i1,...,in)</literal> tem a seguinte
393             seqüência de chamamento:
394             <literal>[x1,..,xm]=%&lt;tipo_de_a&gt;_e_(i1,...,in,a)</literal>
395         </para>
396         <para>A função de sobrecarga associada à sintaxe de inserção
397             <literal>a(i1,...,in)=b</literal> tem a segunite seqüência de
398             chamamento:<literal>
399                 a=%&lt;tipo_de_b&gt;_i_&lt;tipo_de_a&gt;(i1,...,in,b,a)
400             </literal>
401             .
402         </para>
403         <para>
404             O código "char" <literal>6</literal> pode ser usado para alguns
405             algoritmos de inserção complexos como <code>x.b(2) = 33</code> onde o
406             campo <literal>b</literal> não está definido na estrutura
407             <literal>x</literal>. A inserção é automaticamente decomposta em
408             <literal>temp=x.b;</literal> <code>temp(2) = 33</code>;
409             <code>x.b = temp</code>. O código "char" <literal>6</literal> é usado
410             para o primeiro passo desse algoritmo. A função de sobrecarga de
411             <literal>6</literal> é muito semelhante à função de
412             <literal>e</literal>'s.
413         </para>
414         <variablelist>
415             <varlistentry>
416                 <term>Funções (Functions)</term>
417                 <listitem>
418                     <para>algumas funções primitivas básicas também podem ser
419                         sobrecarregadas para novos tipo de dados. Quando tal função não é
420                         definida para tipos de dados particulares, a função <literal>
421                             %&lt;tipo_de_um_argumento&gt;_&lt;nome_da_função&gt;
422                         </literal>
423                         é
424                         chamada. O usuário pode adicionar a esta função chamada a definição
425                         associada aos tipos de dados de entrada.
426                     </para>
427                 </listitem>
428             </varlistentry>
429         </variablelist>
430     </refsection>
431     <refsection>
432         <title>Exemplos</title>
433     </refsection>
434     <refsection>
435         <programlisting role="example">//EXIBIÇÃO
436             var = tlist('tab', ['a', 'b'], ['x'; 'y'], rand(2, 2)) // the type of var is 'tab'
437
438             typeof(var)
439
440             function [] = %tab_p(l)
441             disp([[' '; l(3)] [l(2); string(l(4))]])
442             endfunction
443
444             var // after overloading
445         </programlisting>
446     </refsection>
447     <refsection>
448         <programlisting role="example">//OPERADOR
449             's' + 1 // it is impossible to add a number to a string
450
451             function x = %c_a_s(a, b)
452             x = a + string(b);
453             endfunction
454
455             's' + 1 // after overloading
456         </programlisting>
457     </refsection>
458     <refsection>
459         <programlisting role="example">//FUNÇÃO
460             sin('2 * x') // the sin function does not work with a string
461
462             function x = %c_sin(a)
463             x = 'sin(' + a + ')'
464             endfunction
465
466             sin('2 * x') // after overloading
467         </programlisting>
468     </refsection>
469     <refsection>
470         <title>Ver Também</title>
471         <simplelist type="inline">
472             <member>
473                 <link linkend="tlist">tlist</link>
474             </member>
475             <member>
476                 <link linkend="disp">disp</link>
477             </member>
478             <member>
479                 <link linkend="symbols">symbols</link>
480             </member>
481             <member>
482                 <link linkend="typeof">typeof</link>
483             </member>
484             <member>
485                 <link linkend="type">type</link>
486             </member>
487             <member>
488                 <link linkend="typename">typename</link>
489             </member>
490         </simplelist>
491     </refsection>
492 </refentry>