    [NAME]
    ALL.dao.tutorial.types

    [TITLE]
    基本类型

    [DESCRIPTION]

     0.1   数字
    道语言对以下数字类型有内在支持: int, float, double, complex 和 long。

    道语言里整数和浮点数的表达方式跟其他语言一样。 不过浮点数被默认为双精度 double(从发布
    dao-2.0-devel-2013-06-16开始)。 要显示表示单(双)精度浮点数 float( double), 有必要在整数
    和浮点数后附加 F ( D)后缀。

    The imaginary part of a complex number can be expressed as an int or float number with a
    C suffix. A general complex number can be expressed by combining an integer or a
    floating point number as the real part and an imginary part, which are both stored as
    double precision floating point numbers.

    A complex number can be expressed as an int or float number with a $ suffix.

    long type represents arbitrary precision integers. They can be expressed as an int
    number with a L or a Lx suffix, where x is an integer between 2 and 16 (inclusive),
    representing the base of the number (used for parsing and printing only).

    Examples,

       1  I1 = 123
       2  I2 = 0xabc
       3
       4  F1 = 456.7
       5  F2 = 123e4
       6
       7  D1 = 123D
       8  D2 = 456.7D
       9  D3 = 123E4
      10
      11  C1 = 123$
      12  C2 = 456.6$
      13
      14  L1 = 123456789L
      15  L2 = 10001001110L2
      16  L3 = 0x123abc456L16

    These types support most of the common operators that are meaningful for them. For
    example, all the basic arithmetic operators such as: + (addition, unary plus), -
    (subtraction, unary minus), * (multiplication), / (division), % (modulo) and ** (power)
    are supported.

       1  I1 = 123 + 456
       2  I2 = 789 % 123
       3  F1 = 123.5 ** 3
       4  D1 = 789.5D / 123
       5  L1 = 123456789L ** 123L

    Please see dao.type.int, dao.type.float, dao.type.double, dao.type.complex and dao.type.long
    for more information.

     0.2   Strings

    In Dao, a string can be expressed as a sequence of characters enclosed by a pair of
    single qutotation marks or a pair of double quotation marks. The characters placed
    between the quotation marks must be formated according some rules (such as escaping
    quotation marks and other special characters and using numeric encodings of characters
    etc.). Please see dao.type.string for more information.

       1  mbs = 'str'
       2  wcs = "道语言"
       3  mbs2 = 'It\'s green'
       4  wcs2 = "\u9053\u8bed\u8a00" # the same as wcs;

    To use strings as it is written without special treatment of the characters, they can be
    expressed as verbatim string, which are quoted with a pair of identical compound marks
    in the forms of @[] and @@[]. Any number of letters, digits, underscores, blank spaces,
    dots, colons, dashes and assignment marks can be placed in between the squared brackets
    to make sure the marks will not appear inside the string. The difference between using
    @[] and @@[] is the same as the difference between using single quotation marks and
    double quotation marks. Please see dao.type.string for more information.

       1  # C++ codes in MBS:
       2  cpp =
       3  @[cpp x]
       4  class AA
       5  {
       6  	int index;
       7  };
       8  struct BB{};
       9  @[cpp x]
      10
      11
      12  # Lua codes in WCS:
      13  lua =
      14  @@[lua]
      15  local a = 1;
      16  function Test()
      17  	io.write( 'Hello' )
      18  end
      19  @@[lua]

    The content of a string can be accessed or altered using sub-indexing or slicing:

       1  str = 'ABCDEFGHIJK';
       2
       3  io.writeln( str[1] )    # the second character;
       4  io.writeln( str[:4] )   # the substring from the start to the 4th character;
       5  io.writeln( str[6:] )   # the substring from the 6th character to the end;
       6  io.writeln( str[3:8] )  # the substring from the 3rd to the 8th character;
       7
       8  # Set single character:
       9  str[1] = 'X';
      10  str[1] = 'X'[0];
      11
      12  # Set a substring:
      13  str[2:5] = '1234'   # str = 'AB1234GHIJK'
      14  str[2:5] = '123456' # str = 'AB123456GHIJK'
      15  str[2:5] = '12'     # str = 'AB12GHIJK'
      16
      17  # Using negative index:
      18  io.writeln( str[-1] )    # the last character;
      19  io.writeln( str[-2] )    # the second last character;
      20  io.writeln( str[-2:] )   # the last two characters;

    String can be concaternated using + or +=,

       1  str = 'ABCDEFGHIJK';
       2
       3  str2 = str + '123'  # str2 = ABCDEFGHIJK123
       4
       5  # Append a string:
       6  str += '123' # str = ABCDEFGHIJK123
       7
       8  # Append a character:
       9  str += 88    # str = ABCDEFGHIJK123X

     0.3   Arrays

    Dao has built-in support for multi-dimensional numeric arrays. Such arrays can be defined
    by using the squared brackets [] or array{}. Using such constructs, one can either
    enumerate all the elements as a vector/matrix, or specify an arithmetic progression with
    a start value, a step value and the number of steps. If the step value is omitted, it
    will be assumed to be one.

       1  vec1 = [1, 2, 3]  # array<int> vector, or 1x3 matrix;
       2  vec2 = [1.0; 2; 3]  # array<float> 3x1 matrix, or transposed vector;
       3  mat1 = [1D, 2; 3, 4]  # array<double> 2x2 matrix;
       4  mat2 = [ [1, 2], [3, 4] ]  # 2x2 matrix
       5  mat3 = [ 5 ~ [1, 2, 3] ]   # 5x3 matrix;
       6  mat4 = array{ 1, 2; 3, 4 }  # 2x2 matrix

    Like string, array support sub-indexing, slicing and negative indices:

       1  mat = [ 1, 2, 3; 4, 5, 6; 7, 8, 9 ];  # 3x3 matrix;
       2
       3  rowvec = mat[1,:]  # the second row;
       4  colvec = mat[:,1]  # the second column;
       5
       6  submat1 = mat[:1,:]  # the first two rows;
       7  submat2 = mat[:,1:]  # the last two columns;
       8  submat3 = mat[:1,1:] # intersection between the first two rows and the last two columns;
       9
      10  mat[0,:]  = [11, 22, 33]  # set the first row to [11, 22, 33];
      11  mat[:,1] += [11, 22, 33]  # add [11, 22, 33] to the second column;
      12  mat[:,1] += 100           # add 100 to the second column;
      13
      14  mat[:1,1:] += [10, 20; 30, 40]  # add [10, 20; 30, 40] to sub-matrix of mat;

    Please see dao.type.array for more information.

     0.4   Lists

    List can be created in similar ways as array, by enumerating elements or specifying an
    arithmetic progression, but using {} or list{} instead of [] or array{}. To use
    arithmetic progression, list{} must be used to avoid ambiguity with hash maps.

       1  list1 = { 1, 2, 3 }    # list<int>
       2  list2 = { 1.0, 2, 3 }  # list<float>
       3  list3 = { 5 ~ 1 ~ 2 }  # list<int>
       4
       5  list4 = { 'abc', 'def' }  # list<string>
       6  list5 = { 123, 'abc' }    # list<any>
       7
       8  list6 = list{ 3 ~ 'a' }    # { 'a', 'a', 'a' }
       9  list7 = { 3 ~ 'a' ~ 'b' }  # { 'a', 'ab', 'abb' }

    List also supports sub-indexing, slicing and negative indices:

       1  alist = { 0, 1, 2, 3, 4, 5 }
       2
       3  item = alist[1]
       4  item = alist[-2]
       5
       6  sublist = alist[2:4]
       7
       8  alist[3]  = 10
       9  alist[4] += 10

     0.5   Maps and hash maps

    A map or hash map organize a set of key/value pairs into a structure for efficient
    lookup. The keys in a map are ordered, while the keys in a hash map is unordered. A map
    can be created using {key=>value...} or map{key=>value...}. Replacing the => will create
    hash maps. Map and hash map can be used in identical ways.

       1  amap = { 'abc' => 123, 'def' => 456 }
       2  ahash = { 'abc' : 123, 'def' : 456 }
       3
       4  amap = map{ 'abc' => 123, 'def' => 456 }
       5  ahash = map{ 'abc' : 123, 'def' : 456 }

    Sub-scripting and slicing are also supported for map to access value(s) through key(s).

       1  amap = { 'abc' => 123, 'def' => 456, 'ghi' => 789 }
       2
       3  value  = amap[ 'abc' ];
       4  submap = amap[ 'abc' : 'def' ];

     0.6   Tuples

    Tuple is a very handy type, which can be used to hold a fixed number of items, with type
    information recorded for each of them. In a tuple, each item can have a name, which can
    be used to access the item as field. They can be created in similar ways as creating
    lists and maps, but use () instead.

       1  tup1 = ( 123, 'abc' )               # tuple with unnamed items;
       2  tup2 = ( index => 123, 'abc' )      # the first item is named as "index";
       3  tup3 = tuple{ 123, name => 'abc' }  # the second item is named as "name";

    Each item of a tuple can be accessed using its index or field (item name). New tuples can
    be created from other tuples by slicing.

       1  tup = ( index => 123, 'abc', [1,2,3] )
       2
       3  id = tup[0]
       4  id = tup.index
       5  tup.index = 456
       6
       7  tup2 = tup[:1]  # ( index => 123, 'abc' )