<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( https://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) INRIA
 * Copyright (C) 2017 - Samuel GOUGEON
 *
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 -->
<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:svg="http://www.w3.org/2000/svg" xmlns:mml="http://www.w3.org/1998/Math/MathML"
    xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org"
    xml:lang="en" xml:id="type">
    <refnamediv>
        <refname>type</refname>
        <refpurpose>returns the type of a Scilab object</refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>i = type(x)</synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>x</term>
                <listitem>
                    <para>a Scilab object: a variable or valid expression.</para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>i</term>
                <listitem>
                    <para>
                        integer code identiying the type of <varname>x</varname>.
                    </para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection>
        <title>Description</title>
        <para>
            <code>type(x)</code>  returns an integer which is the type of <varname>x</varname>,
            defined as following:
        </para>
        <variablelist>
            <varlistentry>
                <term>1</term>
                <listitem>decimal or complex numbers
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>2</term>
                <listitem>
                    <link linkend="poly">polynomials</link> with real or complex coefficients
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>4</term>
                <listitem>
                    booleans
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>5</term>
                <listitem>
                    a <link linkend="sparse">sparse matrix</link>.
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>6</term>
                <listitem>
                    a <link linkend="sparse">sparse boolean matrix</link>.
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>7</term>
                <listitem>
                    Matlab sparse matrix
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>8</term>
                <listitem>
                    <link linkend="int8">integers</link> stored on 1 (<literal>int8</literal>), 2 (<literal>int16</literal>), 4 (<literal>int32</literal>), or 8 (<literal>int64</literal>) bytes.
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>9</term>
                <listitem>
                    <link linkend="graphics_entities">graphic handles</link>.
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>10</term>
                <listitem>
                    <link linkend="strings">text objects</link>.
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>13</term>
                <listitem>
                    a compiled <link linkend="function">function</link> in Scilab code.
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>14</term>
                <listitem>
                    <link linkend="library">library</link> of compiled Scilab functions.
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>15</term>
                <listitem>
                    a simple <link linkend="list">list</link>.
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>16</term>
                <listitem>
                    a typed list (<link linkend="tlist">tlist</link>).
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>17</term>
                <listitem>
                    a matrix-oriented typed list (<link linkend="mlist">mlist</link>).
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>128</term>
                <listitem>
                    an identifier of a Xcos block, of a <link linkend="lufact">lufact</link>()'s result,
                    etc.
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>129</term>
                <listitem>
                    a size implicit polynomial used for indexing.
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>130</term>
                <listitem>
                    a built-in Scilab function, aka <emphasis role="italic">gateway</emphasis> (C, C++ or Fortran code).
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>0</term>
                <listitem>
                    An undefined element of a list (with a typeof <literal>void</literal>, such as
                    in the function call <literal>foo(a,,b)</literal>, or in
                    <literal>list(%pi,,%i,,,%z)</literal>), or a null object of typeof
                    <literal>listdelete</literal> returned by <literal>null()</literal>.
                </listitem>
            </varlistentry>
        </variablelist>
        <refsect2>
            <title>Remarks</title>
            <para>
                Some data may be stored in custom containers defined as typed lists of type 16
                (tlist) or 17 (matrix-oriented typed mlist). This is also the case for embedded
                types like (<emphasis role="italic">rationals</emphasis>), structures
                (<emphasis role="italic">struct</emphasis>),
                <emphasis role="italic">state-space</emphasis>, interactive trees
                (<emphasis role="italic">uitree</emphasis>), etc, for which
                <literal>type(..)</literal> will return 16 or 17. One will use
                <link linkend="typeof">typeof</link> to get more information about their actual
                (sub)type.
            </para>
            <para>
                <emphasis role="bold">Type conversion in mixed operations</emphasis>:
            </para>
            <para>
                When a binary operator (like the multiplication) or an n-ary iterated one
                (like the concatenation) is applied between two operands of distinct but
                compatible types, usually the type of one of both is implicitly converted,
                and the other operand sets the type of the result to its own type.
                <para>
                    Examples with the <link linkend="brackets">concatenation</link> :
                    <literal>[%t, 3.124]</literal>, <literal>[int8(5), 3.124]</literal>, etc.
                </para>
                <para>
                For the concatenation, addition, substraction, multiplication, division,
                and the power operation, the hierarchy between operands types is as follows
                (read "A &lt; B": B imposes its type to A) :
                <simplelist>
                    <member>
                        boolean &lt; (decimal number, complex encoded number)
                    </member>
                    <member>
                        <warning>
                            (decimal number, complex-encoded number) &lt; encoded integer.
                        </warning>
                    </member>
                    <member>
                        polynomial (real | complex) &lt; rational (real | complex)
                    </member>
                </simplelist>
                </para>
            </para>
        </refsect2>
    </refsection>
    <refsection>
        <title>Examples</title>
        <programlisting role="example"><![CDATA[
// (Select some lines, right-click and choose "Execute.." to run the selection)
L = list(%pi,,"abc");
type(L(2))                  // undefined component of a list

type(42)                    // decimal numbers
type(%nan)
type(%inf)

type(1+%i)                  // complex numbers

s = sparse([1,2;4,5;3,10],[1,2,3]);
type(s)                     // sparse-encoded decimal or complex numbers

type(%t)                    // booleans

type(s>2)                   // sparse-encoded booleans

g = int8([1 -120 127 312]); // encoded integers
type(g)
type(1.23 * int8(4))

type(1-%z+%z^2)             // polynomials

type(gdf())                 // graphic handles

type("foo")                 // texts

deff('[x] = mymacro(y,z)',['a=3*y+1'; 'x=a*z+y']);
type(mymacro)               // Scilab functions

type(disp)                  // Built-in functions

l = list(1,["a" "b"]);      // simple lists
type(l)

e = tlist(["listtype","field1","field2"], [], []);
type(e)                     // Typed list

h = mlist(['V','name','value'],['a','b';'c' 'd'],[1 2; 3 4]);
type(h)                     // Typed matrix-oriented list

clear s, s.r = %pi          // structures
type(s)

c = {%t %pi %i %z "abc" s}  // cells
type(c)

r = [%z/(1-%z) (1-%z)/%z^2]
type(r)                     // rational fractions
]]></programlisting>
    </refsection>
    <refsection role="see also">
        <title>See also</title>
        <simplelist type="inline">
            <member>
                <link linkend="typeof">typeof</link>
            </member>
            <member>
                <link linkend="inttype">inttype</link>
            </member>
            <member>
                <link linkend="isreal">isreal</link>
            </member>
            <member>
                <link linkend="brackets">brackets</link>
            </member>
            <member>
                <link linkend="overloading">overloading</link>
            </member>
            <member>
                <link linkend="poly">poly</link>
            </member>
            <member>
                <link linkend="rational">rational</link>
            </member>
            <member>
                <link linkend="tlist">tlist</link>
            </member>
            <member>
                <link linkend="mlist">mlist</link>
            </member>
            <member>
                <link linkend="null">null</link>
            </member>
        </simplelist>
    </refsection>
    <refsection role="history">
        <title>History</title>
        <revhistory>
            <revision>
                <revnumber>6.0</revnumber>
                <revdescription>
                  The type 11 is removed. It was used for uncompiled Scilab functions.
                </revdescription>
            </revision>
        </revhistory>
    </refsection>
</refentry>
