summary refs log tree commit diff stats
path: root/tests/assign/tobjasgn.nim
blob: 5f411063f272781194fa6e5ddc146b30475d11a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
discard """
  output: '''0
pre test a:test b:1 c:2 haha:3
assignment test a:test b:1 c:2 haha:3
'''
"""

type TSomeObj = object of TObject
  Variable: int
 
var a = TSomeObj()
 
echo a.Variable.`$`

# bug #575

type
  Something = object of Tobject
    a: string
    b, c: int32

type
  Other = object of Something
    haha: int

proc `$`(x: Other): string =
  result = "a:" & x.a & " b:" & $x.b & " c:" & $x.c & " haha:" & $x.haha

var
  t: Other

t.a = "test"
t.b = 1
t.c = 2
t.haha = 3

echo "pre test ", $t
var x = t
echo "assignment test ", x
{ color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
The set type models the mathematical notion of a set. The set's basetype can
only be an ordinal type of a certain size, namely:
  * ``int8``-``int16``
  * ``uint8``/``byte``-``uint16``
  * ``char``
  * ``enum``
or equivalent. The reason is that sets are implemented as high
performance bit vectors. Attempting to declare a set with a larger type will
result in an error:

.. code-block:: nim

  var s: set[int64] # Error: set is too large

Sets can be constructed via the set constructor: ``{}`` is the empty set. The
empty set is type compatible with any concrete set type. The constructor
can also be used to include elements (and ranges of elements):

.. code-block:: nim
  type
    CharSet = set[char]
  var
    x: CharSet
  x = {'a'..'z', '0'..'9'} # This constructs a set that contains the
                           # letters from 'a' to 'z' and the digits
                           # from '0' to '9'

These operations are supported by sets:

==================    ========================================================
operation             meaning
==================    ========================================================
``A + B``             union of two sets
``A * B``             intersection of two sets
``A - B``             difference of two sets (A without B's elements)
``A == B``            set equality
``A <= B``            subset relation (A is subset of B or equal to B)
``A < B``             strong subset relation (A is a real subset of B)
``e in A``            set membership (A contains element e)
``e notin A``         A does not contain element e
``contains(A, e)``    A contains element e
``card(A)``           the cardinality of A (number of elements in A)
``incl(A, elem)``     same as ``A = A + {elem}``
``excl(A, elem)``     same as ``A = A - {elem}``
==================    ========================================================

Sets are often used to define a type for the *flags* of a procedure. This is
a much cleaner (and type safe) solution than just defining integer
constants that should be ``or``'ed together.