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