types.txt 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. Types
  2. =====
  3. All expressions have a type which is known at compile time. Nim
  4. is statically typed. One can declare new types, which is in essence defining
  5. an identifier that can be used to denote this custom type.
  6. These are the major type classes:
  7. * ordinal types (consist of integer, bool, character, enumeration
  8. (and subranges thereof) types)
  9. * floating point types
  10. * string type
  11. * structured types
  12. * reference (pointer) type
  13. * procedural type
  14. * generic type
  15. Ordinal types
  16. -------------
  17. Ordinal types have the following characteristics:
  18. - Ordinal types are countable and ordered. This property allows
  19. the operation of functions as ``inc``, ``ord``, ``dec`` on ordinal types to
  20. be defined.
  21. - Ordinal values have a smallest possible value. Trying to count further
  22. down than the smallest value gives a checked runtime or static error.
  23. - Ordinal values have a largest possible value. Trying to count further
  24. than the largest value gives a checked runtime or static error.
  25. Integers, bool, characters and enumeration types (and subranges of these
  26. types) belong to ordinal types. For reasons of simplicity of implementation
  27. the types ``uint`` and ``uint64`` are not ordinal types.
  28. Pre-defined integer types
  29. -------------------------
  30. These integer types are pre-defined:
  31. ``int``
  32. the generic signed integer type; its size is platform dependent and has the
  33. same size as a pointer. This type should be used in general. An integer
  34. literal that has no type suffix is of this type.
  35. intXX
  36. additional signed integer types of XX bits use this naming scheme
  37. (example: int16 is a 16 bit wide integer).
  38. The current implementation supports ``int8``, ``int16``, ``int32``, ``int64``.
  39. Literals of these types have the suffix 'iXX.
  40. ``uint``
  41. the generic `unsigned integer`:idx: type; its size is platform dependent and
  42. has the same size as a pointer. An integer literal with the type
  43. suffix ``'u`` is of this type.
  44. uintXX
  45. additional signed integer types of XX bits use this naming scheme
  46. (example: uint16 is a 16 bit wide unsigned integer).
  47. The current implementation supports ``uint8``, ``uint16``, ``uint32``,
  48. ``uint64``. Literals of these types have the suffix 'uXX.
  49. Unsigned operations all wrap around; they cannot lead to over- or
  50. underflow errors.
  51. In addition to the usual arithmetic operators for signed and unsigned integers
  52. (``+ - *`` etc.) there are also operators that formally work on *signed*
  53. integers but treat their arguments as *unsigned*: They are mostly provided
  54. for backwards compatibility with older versions of the language that lacked
  55. unsigned integer types. These unsigned operations for signed integers use
  56. the ``%`` suffix as convention:
  57. ====================== ======================================================
  58. operation meaning
  59. ====================== ======================================================
  60. ``a +% b`` unsigned integer addition
  61. ``a -% b`` unsigned integer subtraction
  62. ``a *% b`` unsigned integer multiplication
  63. ``a /% b`` unsigned integer division
  64. ``a %% b`` unsigned integer modulo operation
  65. ``a <% b`` treat ``a`` and ``b`` as unsigned and compare
  66. ``a <=% b`` treat ``a`` and ``b`` as unsigned and compare
  67. ``ze(a)`` extends the bits of ``a`` with zeros until it has the
  68. width of the ``int`` type
  69. ``toU8(a)`` treats ``a`` as unsigned and converts it to an
  70. unsigned integer of 8 bits (but still the
  71. ``int8`` type)
  72. ``toU16(a)`` treats ``a`` as unsigned and converts it to an
  73. unsigned integer of 16 bits (but still the
  74. ``int16`` type)
  75. ``toU32(a)`` treats ``a`` as unsigned and converts it to an
  76. unsigned integer of 32 bits (but still the
  77. ``int32`` type)
  78. ====================== ======================================================
  79. `Automatic type conversion`:idx: is performed in expressions where different
  80. kinds of integer types are used: the smaller type is converted to the larger.
  81. A `narrowing type conversion`:idx: converts a larger to a smaller type (for
  82. example ``int32 -> int16``. A `widening type conversion`:idx: converts a
  83. smaller type to a larger type (for example ``int16 -> int32``). In Nim only
  84. widening type conversions are *implicit*:
  85. .. code-block:: nim
  86. var myInt16 = 5i16
  87. var myInt: int
  88. myInt16 + 34 # of type ``int16``
  89. myInt16 + myInt # of type ``int``
  90. myInt16 + 2i32 # of type ``int32``
  91. However, ``int`` literals are implicitly convertible to a smaller integer type
  92. if the literal's value fits this smaller type and such a conversion is less
  93. expensive than other implicit conversions, so ``myInt16 + 34`` produces
  94. an ``int16`` result.
  95. For further details, see `Convertible relation`_.
  96. Subrange types
  97. --------------
  98. A subrange type is a range of values from an ordinal type (the base
  99. type). To define a subrange type, one must specify it's limiting values: the
  100. lowest and highest value of the type:
  101. .. code-block:: nim
  102. type
  103. Subrange = range[0..5]
  104. ``Subrange`` is a subrange of an integer which can only hold the values 0
  105. to 5. Assigning any other value to a variable of type ``Subrange`` is a
  106. checked runtime error (or static error if it can be statically
  107. determined). Assignments from the base type to one of its subrange types
  108. (and vice versa) are allowed.
  109. A subrange type has the same size as its base type (``int`` in the example).
  110. Nim requires `interval arithmetic`:idx: for subrange types over a set
  111. of built-in operators that involve constants: ``x %% 3`` is of
  112. type ``range[0..2]``. The following built-in operators for integers are
  113. affected by this rule: ``-``, ``+``, ``*``, ``min``, ``max``, ``succ``,
  114. ``pred``, ``mod``, ``div``, ``%%``, ``and`` (bitwise ``and``).
  115. Bitwise ``and`` only produces a ``range`` if one of its operands is a
  116. constant *x* so that (x+1) is a power of two.
  117. (Bitwise ``and`` is then a ``%%`` operation.)
  118. This means that the following code is accepted:
  119. .. code-block:: nim
  120. case (x and 3) + 7
  121. of 7: echo "A"
  122. of 8: echo "B"
  123. of 9: echo "C"
  124. of 10: echo "D"
  125. # note: no ``else`` required as (x and 3) + 7 has the type: range[7..10]
  126. Pre-defined floating point types
  127. --------------------------------
  128. The following floating point types are pre-defined:
  129. ``float``
  130. the generic floating point type; its size is platform dependent
  131. (the compiler chooses the processor's fastest floating point type).
  132. This type should be used in general.
  133. floatXX
  134. an implementation may define additional floating point types of XX bits using
  135. this naming scheme (example: float64 is a 64 bit wide float). The current
  136. implementation supports ``float32`` and ``float64``. Literals of these types
  137. have the suffix 'fXX.
  138. Automatic type conversion in expressions with different kinds
  139. of floating point types is performed: See `Convertible relation`_ for further
  140. details. Arithmetic performed on floating point types follows the IEEE
  141. standard. Integer types are not converted to floating point types automatically
  142. and vice versa.
  143. The IEEE standard defines five types of floating-point exceptions:
  144. * Invalid: operations with mathematically invalid operands,
  145. for example 0.0/0.0, sqrt(-1.0), and log(-37.8).
  146. * Division by zero: divisor is zero and dividend is a finite nonzero number,
  147. for example 1.0/0.0.
  148. * Overflow: operation produces a result that exceeds the range of the exponent,
  149. for example MAXDOUBLE+0.0000000000001e308.
  150. * Underflow: operation produces a result that is too small to be represented
  151. as a normal number, for example, MINDOUBLE * MINDOUBLE.
  152. * Inexact: operation produces a result that cannot be represented with infinite
  153. precision, for example, 2.0 / 3.0, log(1.1) and 0.1 in input.
  154. The IEEE exceptions are either ignored at runtime or mapped to the
  155. Nim exceptions: `FloatInvalidOpError`:idx:, `FloatDivByZeroError`:idx:,
  156. `FloatOverflowError`:idx:, `FloatUnderflowError`:idx:,
  157. and `FloatInexactError`:idx:.
  158. These exceptions inherit from the `FloatingPointError`:idx: base class.
  159. Nim provides the pragmas `NaNChecks`:idx: and `InfChecks`:idx: to control
  160. whether the IEEE exceptions are ignored or trap a Nim exception:
  161. .. code-block:: nim
  162. {.NanChecks: on, InfChecks: on.}
  163. var a = 1.0
  164. var b = 0.0
  165. echo b / b # raises FloatInvalidOpError
  166. echo a / b # raises FloatOverflowError
  167. In the current implementation ``FloatDivByZeroError`` and ``FloatInexactError``
  168. are never raised. ``FloatOverflowError`` is raised instead of
  169. ``FloatDivByZeroError``.
  170. There is also a `floatChecks`:idx: pragma that is a short-cut for the
  171. combination of ``NaNChecks`` and ``InfChecks`` pragmas. ``floatChecks`` are
  172. turned off as default.
  173. The only operations that are affected by the ``floatChecks`` pragma are
  174. the ``+``, ``-``, ``*``, ``/`` operators for floating point types.
  175. An implementation should always use the maximum precision available to evaluate
  176. floating pointer values at compile time; this means expressions like
  177. ``0.09'f32 + 0.01'f32 == 0.09'f64 + 0.01'f64`` are true.
  178. Boolean type
  179. ------------
  180. The boolean type is named `bool`:idx: in Nim and can be one of the two
  181. pre-defined values ``true`` and ``false``. Conditions in ``while``,
  182. ``if``, ``elif``, ``when``-statements need to be of type ``bool``.
  183. This condition holds::
  184. ord(false) == 0 and ord(true) == 1
  185. The operators ``not, and, or, xor, <, <=, >, >=, !=, ==`` are defined
  186. for the bool type. The ``and`` and ``or`` operators perform short-cut
  187. evaluation. Example:
  188. .. code-block:: nim
  189. while p != nil and p.name != "xyz":
  190. # p.name is not evaluated if p == nil
  191. p = p.next
  192. The size of the bool type is one byte.
  193. Character type
  194. --------------
  195. The character type is named ``char`` in Nim. Its size is one byte.
  196. Thus it cannot represent an UTF-8 character, but a part of it.
  197. The reason for this is efficiency: for the overwhelming majority of use-cases,
  198. the resulting programs will still handle UTF-8 properly as UTF-8 was specially
  199. designed for this.
  200. Another reason is that Nim can support ``array[char, int]`` or
  201. ``set[char]`` efficiently as many algorithms rely on this feature. The
  202. `Rune` type is used for Unicode characters, it can represent any Unicode
  203. character. ``Rune`` is declared in the `unicode module <unicode.html>`_.
  204. Enumeration types
  205. -----------------
  206. Enumeration types define a new type whose values consist of the ones
  207. specified. The values are ordered. Example:
  208. .. code-block:: nim
  209. type
  210. Direction = enum
  211. north, east, south, west
  212. Now the following holds::
  213. ord(north) == 0
  214. ord(east) == 1
  215. ord(south) == 2
  216. ord(west) == 3
  217. Thus, north < east < south < west. The comparison operators can be used
  218. with enumeration types.
  219. For better interfacing to other programming languages, the fields of enum
  220. types can be assigned an explicit ordinal value. However, the ordinal values
  221. have to be in ascending order. A field whose ordinal value is not
  222. explicitly given is assigned the value of the previous field + 1.
  223. An explicit ordered enum can have *holes*:
  224. .. code-block:: nim
  225. type
  226. TokenType = enum
  227. a = 2, b = 4, c = 89 # holes are valid
  228. However, it is then not an ordinal anymore, so it is not possible to use these
  229. enums as an index type for arrays. The procedures ``inc``, ``dec``, ``succ``
  230. and ``pred`` are not available for them either.
  231. The compiler supports the built-in stringify operator ``$`` for enumerations.
  232. The stringify's result can be controlled by explicitly giving the string
  233. values to use:
  234. .. code-block:: nim
  235. type
  236. MyEnum = enum
  237. valueA = (0, "my value A"),
  238. valueB = "value B",
  239. valueC = 2,
  240. valueD = (3, "abc")
  241. As can be seen from the example, it is possible to both specify a field's
  242. ordinal value and its string value by using a tuple. It is also
  243. possible to only specify one of them.
  244. An enum can be marked with the ``pure`` pragma so that it's fields are not
  245. added to the current scope, so they always need to be accessed
  246. via ``MyEnum.value``:
  247. .. code-block:: nim
  248. type
  249. MyEnum {.pure.} = enum
  250. valueA, valueB, valueC, valueD
  251. echo valueA # error: Unknown identifier
  252. echo MyEnum.valueA # works
  253. String type
  254. -----------
  255. All string literals are of the type ``string``. A string in Nim is very
  256. similar to a sequence of characters. However, strings in Nim are both
  257. zero-terminated and have a length field. One can retrieve the length with the
  258. builtin ``len`` procedure; the length never counts the terminating zero.
  259. The assignment operator for strings always copies the string.
  260. The ``&`` operator concatenates strings.
  261. Most native Nim types support conversion to strings with the special ``$`` proc.
  262. When calling the ``echo`` proc, for example, the built-in stringify operation
  263. for the parameter is called:
  264. .. code-block:: nim
  265. echo 3 # calls `$` for `int`
  266. Whenever a user creates a specialized object, implementation of this procedure
  267. provides for ``string`` representation.
  268. .. code-block:: nim
  269. type
  270. Person = object
  271. name: string
  272. age: int
  273. proc `$`(p: Person): string = # `$` always returns a string
  274. result = p.name & " is " &
  275. $p.age & # we *need* the `$` in front of p.age, which
  276. # is natively an integer, to convert it to
  277. # a string
  278. " years old."
  279. While ``$p.name`` can also be used, the ``$`` operation on a string does
  280. nothing. Note that we cannot rely on automatic conversion from an ``int`` to
  281. a ``string`` like we can for the ``echo`` proc.
  282. Strings are compared by their lexicographical order. All comparison operators
  283. are available. Strings can be indexed like arrays (lower bound is 0). Unlike
  284. arrays, they can be used in case statements:
  285. .. code-block:: nim
  286. case paramStr(i)
  287. of "-v": incl(options, optVerbose)
  288. of "-h", "-?": incl(options, optHelp)
  289. else: write(stdout, "invalid command line option!\n")
  290. Per convention, all strings are UTF-8 strings, but this is not enforced. For
  291. example, when reading strings from binary files, they are merely a sequence of
  292. bytes. The index operation ``s[i]`` means the i-th *char* of ``s``, not the
  293. i-th *unichar*. The iterator ``runes`` from the `unicode module
  294. <unicode.html>`_ can be used for iteration over all Unicode characters.
  295. cstring type
  296. ------------
  297. The ``cstring`` type meaning `compatible string` is the native representation
  298. of a string for the compilation backend. For the C backend the ``cstring`` type
  299. represents a pointer to a zero-terminated char array
  300. compatible to the type ``char*`` in Ansi C. Its primary purpose lies in easy
  301. interfacing with C. The index operation ``s[i]`` means the i-th *char* of
  302. ``s``; however no bounds checking for ``cstring`` is performed making the
  303. index operation unsafe.
  304. A Nim ``string`` is implicitly convertible
  305. to ``cstring`` for convenience. If a Nim string is passed to a C-style
  306. variadic proc, it is implicitly converted to ``cstring`` too:
  307. .. code-block:: nim
  308. proc printf(formatstr: cstring) {.importc: "printf", varargs,
  309. header: "<stdio.h>".}
  310. printf("This works %s", "as expected")
  311. Even though the conversion is implicit, it is not *safe*: The garbage collector
  312. does not consider a ``cstring`` to be a root and may collect the underlying
  313. memory. However in practice this almost never happens as the GC considers
  314. stack roots conservatively. One can use the builtin procs ``GC_ref`` and
  315. ``GC_unref`` to keep the string data alive for the rare cases where it does
  316. not work.
  317. A `$` proc is defined for cstrings that returns a string. Thus to get a nim
  318. string from a cstring:
  319. .. code-block:: nim
  320. var str: string = "Hello!"
  321. var cstr: cstring = str
  322. var newstr: string = $cstr
  323. Structured types
  324. ----------------
  325. A variable of a structured type can hold multiple values at the same
  326. time. Structured types can be nested to unlimited levels. Arrays, sequences,
  327. tuples, objects and sets belong to the structured types.
  328. Array and sequence types
  329. ------------------------
  330. Arrays are a homogeneous type, meaning that each element in the array
  331. has the same type. Arrays always have a fixed length which is specified at
  332. compile time (except for open arrays). They can be indexed by any ordinal type.
  333. A parameter ``A`` may be an *open array*, in which case it is indexed by
  334. integers from 0 to ``len(A)-1``. An array expression may be constructed by the
  335. array constructor ``[]``. The element type of this array expression is
  336. inferred from the type of the first element. All other elements need to be
  337. implicitly convertable to this type.
  338. Sequences are similar to arrays but of dynamic length which may change
  339. during runtime (like strings). Sequences are implemented as growable arrays,
  340. allocating pieces of memory as items are added. A sequence ``S`` is always
  341. indexed by integers from 0 to ``len(S)-1`` and its bounds are checked.
  342. Sequences can be constructed by the array constructor ``[]`` in conjunction
  343. with the array to sequence operator ``@``. Another way to allocate space for a
  344. sequence is to call the built-in ``newSeq`` procedure.
  345. A sequence may be passed to a parameter that is of type *open array*.
  346. Example:
  347. .. code-block:: nim
  348. type
  349. IntArray = array[0..5, int] # an array that is indexed with 0..5
  350. IntSeq = seq[int] # a sequence of integers
  351. var
  352. x: IntArray
  353. y: IntSeq
  354. x = [1, 2, 3, 4, 5, 6] # [] is the array constructor
  355. y = @[1, 2, 3, 4, 5, 6] # the @ turns the array into a sequence
  356. let z = [1.0, 2, 3, 4] # the type of z is array[0..3, float]
  357. The lower bound of an array or sequence may be received by the built-in proc
  358. ``low()``, the higher bound by ``high()``. The length may be
  359. received by ``len()``. ``low()`` for a sequence or an open array always returns
  360. 0, as this is the first valid index.
  361. One can append elements to a sequence with the ``add()`` proc or the ``&``
  362. operator, and remove (and get) the last element of a sequence with the
  363. ``pop()`` proc.
  364. The notation ``x[i]`` can be used to access the i-th element of ``x``.
  365. Arrays are always bounds checked (at compile-time or at runtime). These
  366. checks can be disabled via pragmas or invoking the compiler with the
  367. ``--boundChecks:off`` command line switch.
  368. Open arrays
  369. -----------
  370. Often fixed size arrays turn out to be too inflexible; procedures should
  371. be able to deal with arrays of different sizes. The `openarray`:idx: type
  372. allows this; it can only be used for parameters. Openarrays are always
  373. indexed with an ``int`` starting at position 0. The ``len``, ``low``
  374. and ``high`` operations are available for open arrays too. Any array with
  375. a compatible base type can be passed to an openarray parameter, the index
  376. type does not matter. In addition to arrays sequences can also be passed
  377. to an open array parameter.
  378. The openarray type cannot be nested: multidimensional openarrays are not
  379. supported because this is seldom needed and cannot be done efficiently.
  380. .. code-block:: nim
  381. proc testOpenArray(x: openArray[int]) = echo repr(x)
  382. testOpenArray([1,2,3]) # array[]
  383. testOpenArray(@[1,2,3]) # seq[]
  384. Varargs
  385. -------
  386. A ``varargs`` parameter is an openarray parameter that additionally
  387. allows to pass a variable number of arguments to a procedure. The compiler
  388. converts the list of arguments to an array implicitly:
  389. .. code-block:: nim
  390. proc myWriteln(f: File, a: varargs[string]) =
  391. for s in items(a):
  392. write(f, s)
  393. write(f, "\n")
  394. myWriteln(stdout, "abc", "def", "xyz")
  395. # is transformed to:
  396. myWriteln(stdout, ["abc", "def", "xyz"])
  397. This transformation is only done if the varargs parameter is the
  398. last parameter in the procedure header. It is also possible to perform
  399. type conversions in this context:
  400. .. code-block:: nim
  401. proc myWriteln(f: File, a: varargs[string, `$`]) =
  402. for s in items(a):
  403. write(f, s)
  404. write(f, "\n")
  405. myWriteln(stdout, 123, "abc", 4.0)
  406. # is transformed to:
  407. myWriteln(stdout, [$123, $"def", $4.0])
  408. In this example ``$`` is applied to any argument that is passed to the
  409. parameter ``a``. (Note that ``$`` applied to strings is a nop.)
  410. Note that an explicit array constructor passed to a ``varargs`` parameter is
  411. not wrapped in another implicit array construction:
  412. .. code-block:: nim
  413. proc takeV[T](a: varargs[T]) = discard
  414. takeV([123, 2, 1]) # takeV's T is "int", not "array of int"
  415. ``varargs[typed]`` is treated specially: It matches a variable list of arguments
  416. of arbitrary type but *always* constructs an implicit array. This is required
  417. so that the builtin ``echo`` proc does what is expected:
  418. .. code-block:: nim
  419. proc echo*(x: varargs[typed, `$`]) {...}
  420. echo @[1, 2, 3]
  421. # prints "@[1, 2, 3]" and not "123"
  422. Tuples and object types
  423. -----------------------
  424. A variable of a tuple or object type is a heterogeneous storage
  425. container.
  426. A tuple or object defines various named *fields* of a type. A tuple also
  427. defines an *order* of the fields. Tuples are meant for heterogeneous storage
  428. types with no overhead and few abstraction possibilities. The constructor ``()``
  429. can be used to construct tuples. The order of the fields in the constructor
  430. must match the order of the tuple's definition. Different tuple-types are
  431. *equivalent* if they specify the same fields of the same type in the same
  432. order. The *names* of the fields also have to be identical.
  433. The assignment operator for tuples copies each component.
  434. The default assignment operator for objects copies each component. Overloading
  435. of the assignment operator is described in `type-bound-operations-operator`_.
  436. .. code-block:: nim
  437. type
  438. Person = tuple[name: string, age: int] # type representing a person:
  439. # a person consists of a name
  440. # and an age
  441. var
  442. person: Person
  443. person = (name: "Peter", age: 30)
  444. # the same, but less readable:
  445. person = ("Peter", 30)
  446. The implementation aligns the fields for best access performance. The alignment
  447. is compatible with the way the C compiler does it.
  448. For consistency with ``object`` declarations, tuples in a ``type`` section
  449. can also be defined with indentation instead of ``[]``:
  450. .. code-block:: nim
  451. type
  452. Person = tuple # type representing a person
  453. name: string # a person consists of a name
  454. age: natural # and an age
  455. Objects provide many features that tuples do not. Object provide inheritance
  456. and information hiding. Objects have access to their type at runtime, so that
  457. the ``of`` operator can be used to determine the object's type. The ``of`` operator
  458. is similar to the ``instanceof`` operator in Java.
  459. .. code-block:: nim
  460. type
  461. Person = object of RootObj
  462. name*: string # the * means that `name` is accessible from other modules
  463. age: int # no * means that the field is hidden
  464. Student = ref object of Person # a student is a person
  465. id: int # with an id field
  466. var
  467. student: Student
  468. person: Person
  469. assert(student of Student) # is true
  470. assert(student of Person) # also true
  471. Object fields that should be visible from outside the defining module, have to
  472. be marked by ``*``. In contrast to tuples, different object types are
  473. never *equivalent*. Objects that have no ancestor are implicitly ``final``
  474. and thus have no hidden type field. One can use the ``inheritable`` pragma to
  475. introduce new object roots apart from ``system.RootObj``.
  476. Object construction
  477. -------------------
  478. Objects can also be created with an `object construction expression`:idx: that
  479. has the syntax ``T(fieldA: valueA, fieldB: valueB, ...)`` where ``T`` is
  480. an ``object`` type or a ``ref object`` type:
  481. .. code-block:: nim
  482. var student = Student(name: "Anton", age: 5, id: 3)
  483. Note that, unlike tuples, objects require the field names along with their values.
  484. For a ``ref object`` type ``system.new`` is invoked implicitly.
  485. Object variants
  486. ---------------
  487. Often an object hierarchy is overkill in certain situations where simple
  488. variant types are needed.
  489. An example:
  490. .. code-block:: nim
  491. # This is an example how an abstract syntax tree could be modelled in Nim
  492. type
  493. NodeKind = enum # the different node types
  494. nkInt, # a leaf with an integer value
  495. nkFloat, # a leaf with a float value
  496. nkString, # a leaf with a string value
  497. nkAdd, # an addition
  498. nkSub, # a subtraction
  499. nkIf # an if statement
  500. Node = ref NodeObj
  501. NodeObj = object
  502. case kind: NodeKind # the ``kind`` field is the discriminator
  503. of nkInt: intVal: int
  504. of nkFloat: floatVal: float
  505. of nkString: strVal: string
  506. of nkAdd, nkSub:
  507. leftOp, rightOp: Node
  508. of nkIf:
  509. condition, thenPart, elsePart: Node
  510. # create a new case object:
  511. var n = Node(kind: nkIf, condition: nil)
  512. # accessing n.thenPart is valid because the ``nkIf`` branch is active:
  513. n.thenPart = Node(kind: nkFloat, floatVal: 2.0)
  514. # the following statement raises an `FieldError` exception, because
  515. # n.kind's value does not fit and the ``nkString`` branch is not active:
  516. n.strVal = ""
  517. # invalid: would change the active object branch:
  518. n.kind = nkInt
  519. var x = Node(kind: nkAdd, leftOp: Node(kind: nkInt, intVal: 4),
  520. rightOp: Node(kind: nkInt, intVal: 2))
  521. # valid: does not change the active object branch:
  522. x.kind = nkSub
  523. As can been seen from the example, an advantage to an object hierarchy is that
  524. no casting between different object types is needed. Yet, access to invalid
  525. object fields raises an exception.
  526. The syntax of ``case`` in an object declaration follows closely the syntax of
  527. the ``case`` statement: The branches in a ``case`` section may be indented too.
  528. In the example the ``kind`` field is called the `discriminator`:idx:\: For
  529. safety its address cannot be taken and assignments to it are restricted: The
  530. new value must not lead to a change of the active object branch. For an object
  531. branch switch ``system.reset`` has to be used. Also, when the fields of a
  532. particular branch are specified during object construction, the correct value
  533. for the discriminator must be supplied at compile-time.
  534. Set type
  535. --------
  536. .. include:: ../sets_fragment.txt
  537. Reference and pointer types
  538. ---------------------------
  539. References (similar to pointers in other programming languages) are a
  540. way to introduce many-to-one relationships. This means different references can
  541. point to and modify the same location in memory (also called `aliasing`:idx:).
  542. Nim distinguishes between `traced`:idx: and `untraced`:idx: references.
  543. Untraced references are also called *pointers*. Traced references point to
  544. objects of a garbage collected heap, untraced references point to
  545. manually allocated objects or to objects somewhere else in memory. Thus
  546. untraced references are *unsafe*. However for certain low-level operations
  547. (accessing the hardware) untraced references are unavoidable.
  548. Traced references are declared with the **ref** keyword, untraced references
  549. are declared with the **ptr** keyword. In general, a `ptr T` is implicitly
  550. convertible to the `pointer` type.
  551. An empty subscript ``[]`` notation can be used to derefer a reference,
  552. the ``addr`` procedure returns the address of an item. An address is always
  553. an untraced reference.
  554. Thus the usage of ``addr`` is an *unsafe* feature.
  555. The ``.`` (access a tuple/object field operator)
  556. and ``[]`` (array/string/sequence index operator) operators perform implicit
  557. dereferencing operations for reference types:
  558. .. code-block:: nim
  559. type
  560. Node = ref NodeObj
  561. NodeObj = object
  562. le, ri: Node
  563. data: int
  564. var
  565. n: Node
  566. new(n)
  567. n.data = 9
  568. # no need to write n[].data; in fact n[].data is highly discouraged!
  569. Automatic dereferencing is also performed for the first argument of a routine
  570. call. But currently this feature has to be only enabled
  571. via ``{.experimental.}``:
  572. .. code-block:: nim
  573. {.experimental.}
  574. proc depth(x: NodeObj): int = ...
  575. var
  576. n: Node
  577. new(n)
  578. echo n.depth
  579. # no need to write n[].depth either
  580. In order to simplify structural type checking, recursive tuples are not valid:
  581. .. code-block:: nim
  582. # invalid recursion
  583. type MyTuple = tuple[a: ref MyTuple]
  584. Likewise ``T = ref T`` is an invalid type.
  585. As a syntactical extension ``object`` types can be anonymous if
  586. declared in a type section via the ``ref object`` or ``ptr object`` notations.
  587. This feature is useful if an object should only gain reference semantics:
  588. .. code-block:: nim
  589. type
  590. Node = ref object
  591. le, ri: Node
  592. data: int
  593. To allocate a new traced object, the built-in procedure ``new`` has to be used.
  594. To deal with untraced memory, the procedures ``alloc``, ``dealloc`` and
  595. ``realloc`` can be used. The documentation of the system module contains
  596. further information.
  597. If a reference points to *nothing*, it has the value ``nil``.
  598. Special care has to be taken if an untraced object contains traced objects like
  599. traced references, strings or sequences: in order to free everything properly,
  600. the built-in procedure ``GCunref`` has to be called before freeing the untraced
  601. memory manually:
  602. .. code-block:: nim
  603. type
  604. Data = tuple[x, y: int, s: string]
  605. # allocate memory for Data on the heap:
  606. var d = cast[ptr Data](alloc0(sizeof(Data)))
  607. # create a new string on the garbage collected heap:
  608. d.s = "abc"
  609. # tell the GC that the string is not needed anymore:
  610. GCunref(d.s)
  611. # free the memory:
  612. dealloc(d)
  613. Without the ``GCunref`` call the memory allocated for the ``d.s`` string would
  614. never be freed. The example also demonstrates two important features for low
  615. level programming: the ``sizeof`` proc returns the size of a type or value
  616. in bytes. The ``cast`` operator can circumvent the type system: the compiler
  617. is forced to treat the result of the ``alloc0`` call (which returns an untyped
  618. pointer) as if it would have the type ``ptr Data``. Casting should only be
  619. done if it is unavoidable: it breaks type safety and bugs can lead to
  620. mysterious crashes.
  621. **Note**: The example only works because the memory is initialized to zero
  622. (``alloc0`` instead of ``alloc`` does this): ``d.s`` is thus initialized to
  623. ``nil`` which the string assignment can handle. One needs to know low level
  624. details like this when mixing garbage collected data with unmanaged memory.
  625. .. XXX finalizers for traced objects
  626. Not nil annotation
  627. ------------------
  628. All types for that ``nil`` is a valid value can be annotated to
  629. exclude ``nil`` as a valid value with the ``not nil`` annotation:
  630. .. code-block:: nim
  631. type
  632. PObject = ref TObj not nil
  633. TProc = (proc (x, y: int)) not nil
  634. proc p(x: PObject) =
  635. echo "not nil"
  636. # compiler catches this:
  637. p(nil)
  638. # and also this:
  639. var x: PObject
  640. p(x)
  641. The compiler ensures that every code path initializes variables which contain
  642. non nilable pointers. The details of this analysis are still to be specified
  643. here.
  644. Memory regions
  645. --------------
  646. The types ``ref`` and ``ptr`` can get an optional ``region`` annotation.
  647. A region has to be an object type.
  648. Regions are very useful to separate user space and kernel memory in the
  649. development of OS kernels:
  650. .. code-block:: nim
  651. type
  652. Kernel = object
  653. Userspace = object
  654. var a: Kernel ptr Stat
  655. var b: Userspace ptr Stat
  656. # the following does not compile as the pointer types are incompatible:
  657. a = b
  658. As the example shows ``ptr`` can also be used as a binary
  659. operator, ``region ptr T`` is a shortcut for ``ptr[region, T]``.
  660. In order to make generic code easier to write ``ptr T`` is a subtype
  661. of ``ptr[R, T]`` for any ``R``.
  662. Furthermore the subtype relation of the region object types is lifted to
  663. the pointer types: If ``A <: B`` then ``ptr[A, T] <: ptr[B, T]``. This can be
  664. used to model subregions of memory. As a special typing rule ``ptr[R, T]`` is
  665. not compatible to ``pointer`` to prevent the following from compiling:
  666. .. code-block:: nim
  667. # from system
  668. proc dealloc(p: pointer)
  669. # wrap some scripting language
  670. type
  671. PythonsHeap = object
  672. PyObjectHeader = object
  673. rc: int
  674. typ: pointer
  675. PyObject = ptr[PythonsHeap, PyObjectHeader]
  676. proc createPyObject(): PyObject {.importc: "...".}
  677. proc destroyPyObject(x: PyObject) {.importc: "...".}
  678. var foo = createPyObject()
  679. # type error here, how convenient:
  680. dealloc(foo)
  681. Future directions:
  682. * Memory regions might become available for ``string`` and ``seq`` too.
  683. * Builtin regions like ``private``, ``global`` and ``local`` might be
  684. useful for an OpenCL target.
  685. * Builtin "regions" can model ``lent`` and ``unique`` pointers.
  686. * An assignment operator can be attached to a region so that proper write
  687. barriers can be generated. This would imply that the GC can be implemented
  688. completely in user-space.
  689. Procedural type
  690. ---------------
  691. A procedural type is internally a pointer to a procedure. ``nil`` is
  692. an allowed value for variables of a procedural type. Nim uses procedural
  693. types to achieve `functional`:idx: programming techniques.
  694. Examples:
  695. .. code-block:: nim
  696. proc printItem(x: int) = ...
  697. proc forEach(c: proc (x: int) {.cdecl.}) =
  698. ...
  699. forEach(printItem) # this will NOT compile because calling conventions differ
  700. .. code-block:: nim
  701. type
  702. OnMouseMove = proc (x, y: int) {.closure.}
  703. proc onMouseMove(mouseX, mouseY: int) =
  704. # has default calling convention
  705. echo "x: ", mouseX, " y: ", mouseY
  706. proc setOnMouseMove(mouseMoveEvent: OnMouseMove) = discard
  707. # ok, 'onMouseMove' has the default calling convention, which is compatible
  708. # to 'closure':
  709. setOnMouseMove(onMouseMove)
  710. A subtle issue with procedural types is that the calling convention of the
  711. procedure influences the type compatibility: procedural types are only
  712. compatible if they have the same calling convention. As a special extension,
  713. a procedure of the calling convention ``nimcall`` can be passed to a parameter
  714. that expects a proc of the calling convention ``closure``.
  715. Nim supports these `calling conventions`:idx:\:
  716. `nimcall`:idx:
  717. is the default convention used for a Nim **proc**. It is the
  718. same as ``fastcall``, but only for C compilers that support ``fastcall``.
  719. `closure`:idx:
  720. is the default calling convention for a **procedural type** that lacks
  721. any pragma annotations. It indicates that the procedure has a hidden
  722. implicit parameter (an *environment*). Proc vars that have the calling
  723. convention ``closure`` take up two machine words: One for the proc pointer
  724. and another one for the pointer to implicitly passed environment.
  725. `stdcall`:idx:
  726. This the stdcall convention as specified by Microsoft. The generated C
  727. procedure is declared with the ``__stdcall`` keyword.
  728. `cdecl`:idx:
  729. The cdecl convention means that a procedure shall use the same convention
  730. as the C compiler. Under windows the generated C procedure is declared with
  731. the ``__cdecl`` keyword.
  732. `safecall`:idx:
  733. This is the safecall convention as specified by Microsoft. The generated C
  734. procedure is declared with the ``__safecall`` keyword. The word *safe*
  735. refers to the fact that all hardware registers shall be pushed to the
  736. hardware stack.
  737. `inline`:idx:
  738. The inline convention means the the caller should not call the procedure,
  739. but inline its code directly. Note that Nim does not inline, but leaves
  740. this to the C compiler; it generates ``__inline`` procedures. This is
  741. only a hint for the compiler: it may completely ignore it and
  742. it may inline procedures that are not marked as ``inline``.
  743. `fastcall`:idx:
  744. Fastcall means different things to different C compilers. One gets whatever
  745. the C ``__fastcall`` means.
  746. `syscall`:idx:
  747. The syscall convention is the same as ``__syscall`` in C. It is used for
  748. interrupts.
  749. `noconv`:idx:
  750. The generated C code will not have any explicit calling convention and thus
  751. use the C compiler's default calling convention. This is needed because
  752. Nim's default calling convention for procedures is ``fastcall`` to
  753. improve speed.
  754. Most calling conventions exist only for the Windows 32-bit platform.
  755. The default calling convention is ``nimcall``, unless it is an inner proc (a
  756. proc inside of a proc). For an inner proc an analysis is performed whether it
  757. accesses its environment. If it does so, it has the calling convention
  758. ``closure``, otherwise it has the calling convention ``nimcall``.
  759. Distinct type
  760. -------------
  761. A ``distinct`` type is new type derived from a `base type`:idx: that is
  762. incompatible with its base type. In particular, it is an essential property
  763. of a distinct type that it **does not** imply a subtype relation between it
  764. and its base type. Explicit type conversions from a distinct type to its
  765. base type and vice versa are allowed.
  766. Modelling currencies
  767. ~~~~~~~~~~~~~~~~~~~~
  768. A distinct type can be used to model different physical `units`:idx: with a
  769. numerical base type, for example. The following example models currencies.
  770. Different currencies should not be mixed in monetary calculations. Distinct
  771. types are a perfect tool to model different currencies:
  772. .. code-block:: nim
  773. type
  774. Dollar = distinct int
  775. Euro = distinct int
  776. var
  777. d: Dollar
  778. e: Euro
  779. echo d + 12
  780. # Error: cannot add a number with no unit and a ``Dollar``
  781. Unfortunately, ``d + 12.Dollar`` is not allowed either,
  782. because ``+`` is defined for ``int`` (among others), not for ``Dollar``. So
  783. a ``+`` for dollars needs to be defined:
  784. .. code-block::
  785. proc `+` (x, y: Dollar): Dollar =
  786. result = Dollar(int(x) + int(y))
  787. It does not make sense to multiply a dollar with a dollar, but with a
  788. number without unit; and the same holds for division:
  789. .. code-block::
  790. proc `*` (x: Dollar, y: int): Dollar =
  791. result = Dollar(int(x) * y)
  792. proc `*` (x: int, y: Dollar): Dollar =
  793. result = Dollar(x * int(y))
  794. proc `div` ...
  795. This quickly gets tedious. The implementations are trivial and the compiler
  796. should not generate all this code only to optimize it away later - after all
  797. ``+`` for dollars should produce the same binary code as ``+`` for ints.
  798. The pragma `borrow`:idx: has been designed to solve this problem; in principle
  799. it generates the above trivial implementations:
  800. .. code-block:: nim
  801. proc `*` (x: Dollar, y: int): Dollar {.borrow.}
  802. proc `*` (x: int, y: Dollar): Dollar {.borrow.}
  803. proc `div` (x: Dollar, y: int): Dollar {.borrow.}
  804. The ``borrow`` pragma makes the compiler use the same implementation as
  805. the proc that deals with the distinct type's base type, so no code is
  806. generated.
  807. But it seems all this boilerplate code needs to be repeated for the ``Euro``
  808. currency. This can be solved with templates_.
  809. .. code-block:: nim
  810. template additive(typ: typedesc) =
  811. proc `+` *(x, y: typ): typ {.borrow.}
  812. proc `-` *(x, y: typ): typ {.borrow.}
  813. # unary operators:
  814. proc `+` *(x: typ): typ {.borrow.}
  815. proc `-` *(x: typ): typ {.borrow.}
  816. template multiplicative(typ, base: typedesc) =
  817. proc `*` *(x: typ, y: base): typ {.borrow.}
  818. proc `*` *(x: base, y: typ): typ {.borrow.}
  819. proc `div` *(x: typ, y: base): typ {.borrow.}
  820. proc `mod` *(x: typ, y: base): typ {.borrow.}
  821. template comparable(typ: typedesc) =
  822. proc `<` * (x, y: typ): bool {.borrow.}
  823. proc `<=` * (x, y: typ): bool {.borrow.}
  824. proc `==` * (x, y: typ): bool {.borrow.}
  825. template defineCurrency(typ, base: untyped) =
  826. type
  827. typ* = distinct base
  828. additive(typ)
  829. multiplicative(typ, base)
  830. comparable(typ)
  831. defineCurrency(Dollar, int)
  832. defineCurrency(Euro, int)
  833. The borrow pragma can also be used to annotate the distinct type to allow
  834. certain builtin operations to be lifted:
  835. .. code-block:: nim
  836. type
  837. Foo = object
  838. a, b: int
  839. s: string
  840. Bar {.borrow: `.`.} = distinct Foo
  841. var bb: ref Bar
  842. new bb
  843. # field access now valid
  844. bb.a = 90
  845. bb.s = "abc"
  846. Currently only the dot accessor can be borrowed in this way.
  847. Avoiding SQL injection attacks
  848. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  849. An SQL statement that is passed from Nim to an SQL database might be
  850. modelled as a string. However, using string templates and filling in the
  851. values is vulnerable to the famous `SQL injection attack`:idx:\:
  852. .. code-block:: nim
  853. import strutils
  854. proc query(db: DbHandle, statement: string) = ...
  855. var
  856. username: string
  857. db.query("SELECT FROM users WHERE name = '$1'" % username)
  858. # Horrible security hole, but the compiler does not mind!
  859. This can be avoided by distinguishing strings that contain SQL from strings
  860. that don't. Distinct types provide a means to introduce a new string type
  861. ``SQL`` that is incompatible with ``string``:
  862. .. code-block:: nim
  863. type
  864. SQL = distinct string
  865. proc query(db: DbHandle, statement: SQL) = ...
  866. var
  867. username: string
  868. db.query("SELECT FROM users WHERE name = '$1'" % username)
  869. # Error at compile time: `query` expects an SQL string!
  870. It is an essential property of abstract types that they **do not** imply a
  871. subtype relation between the abstract type and its base type. Explicit type
  872. conversions from ``string`` to ``SQL`` are allowed:
  873. .. code-block:: nim
  874. import strutils, sequtils
  875. proc properQuote(s: string): SQL =
  876. # quotes a string properly for an SQL statement
  877. return SQL(s)
  878. proc `%` (frmt: SQL, values: openarray[string]): SQL =
  879. # quote each argument:
  880. let v = values.mapIt(SQL, properQuote(it))
  881. # we need a temporary type for the type conversion :-(
  882. type StrSeq = seq[string]
  883. # call strutils.`%`:
  884. result = SQL(string(frmt) % StrSeq(v))
  885. db.query("SELECT FROM users WHERE name = '$1'".SQL % [username])
  886. Now we have compile-time checking against SQL injection attacks. Since
  887. ``"".SQL`` is transformed to ``SQL("")`` no new syntax is needed for nice
  888. looking ``SQL`` string literals. The hypothetical ``SQL`` type actually
  889. exists in the library as the `TSqlQuery type <db_sqlite.html#TSqlQuery>`_ of
  890. modules like `db_sqlite <db_sqlite.html>`_.
  891. Void type
  892. ---------
  893. The ``void`` type denotes the absence of any type. Parameters of
  894. type ``void`` are treated as non-existent, ``void`` as a return type means that
  895. the procedure does not return a value:
  896. .. code-block:: nim
  897. proc nothing(x, y: void): void =
  898. echo "ha"
  899. nothing() # writes "ha" to stdout
  900. The ``void`` type is particularly useful for generic code:
  901. .. code-block:: nim
  902. proc callProc[T](p: proc (x: T), x: T) =
  903. when T is void:
  904. p()
  905. else:
  906. p(x)
  907. proc intProc(x: int) = discard
  908. proc emptyProc() = discard
  909. callProc[int](intProc, 12)
  910. callProc[void](emptyProc)
  911. However, a ``void`` type cannot be inferred in generic code:
  912. .. code-block:: nim
  913. callProc(emptyProc)
  914. # Error: type mismatch: got (proc ())
  915. # but expected one of:
  916. # callProc(p: proc (T), x: T)
  917. The ``void`` type is only valid for parameters and return types; other symbols
  918. cannot have the type ``void``.
  919. Auto type
  920. ---------
  921. The ``auto`` type can only be used for return types and parameters. For return
  922. types it causes the compiler to infer the type from the routine body:
  923. .. code-block:: nim
  924. proc returnsInt(): auto = 1984
  925. For parameters it currently creates implicitly generic routines:
  926. .. code-block:: nim
  927. proc foo(a, b: auto) = discard
  928. Is the same as:
  929. .. code-block:: nim
  930. proc foo[T1, T2](a: T1, b: T2) = discard
  931. However later versions of the language might change this to mean "infer the
  932. parameters' types from the body". Then the above ``foo`` would be rejected as
  933. the parameters' types can not be inferred from an empty ``discard`` statement.