minor improvements to the docs

This commit is contained in:
Araq 2014-12-14 11:50:10 +01:00
commit 9724bd4e1b
6 changed files with 54 additions and 52 deletions

View file

@ -33,7 +33,7 @@ import
ast, astalgo, strutils, hashes, trees, platform, magicsys, extccomp, ast, astalgo, strutils, hashes, trees, platform, magicsys, extccomp,
options, nversion, nimsets, msgs, crc, bitsets, idents, lists, types, os, options, nversion, nimsets, msgs, crc, bitsets, idents, lists, types, os,
times, ropes, math, passes, ccgutils, wordrecg, renderer, rodread, rodutils, times, ropes, math, passes, ccgutils, wordrecg, renderer, rodread, rodutils,
intsets, cgmeth intsets, cgmeth, lowerings
type type
TTarget = enum TTarget = enum
@ -1211,13 +1211,17 @@ proc genVarInit(p: PProc, v: PSym, n: PNode) =
proc genVarStmt(p: PProc, n: PNode) = proc genVarStmt(p: PProc, n: PNode) =
for i in countup(0, sonsLen(n) - 1): for i in countup(0, sonsLen(n) - 1):
var a = n.sons[i] var a = n.sons[i]
if a.kind == nkCommentStmt: continue if a.kind != nkCommentStmt:
assert(a.kind == nkIdentDefs) if a.kind == nkVarTuple:
assert(a.sons[0].kind == nkSym) let unpacked = lowerTupleUnpacking(a, p.prc)
var v = a.sons[0].sym genStmt(p, unpacked)
if lfNoDecl in v.loc.flags: continue else:
genLineDir(p, a) assert(a.kind == nkIdentDefs)
genVarInit(p, v, a.sons[2]) assert(a.sons[0].kind == nkSym)
var v = a.sons[0].sym
if lfNoDecl notin v.loc.flags:
genLineDir(p, a)
genVarInit(p, v, a.sons[2])
proc genConstant(p: PProc, c: PSym) = proc genConstant(p: PProc, c: PSym) =
if lfNoDecl notin c.loc.flags and not p.g.generatedSyms.containsOrIncl(c.id): if lfNoDecl notin c.loc.flags and not p.g.generatedSyms.containsOrIncl(c.id):

View file

@ -250,7 +250,7 @@ Another common example is this:
yield "Hello" yield "Hello"
yield "World" yield "World"
var info = something().toSeq() var info = toSeq(something())
The problem here is that the compiler already decided that ``something()`` as The problem here is that the compiler already decided that ``something()`` as
an iterator is not callable in this context before ``toSeq`` gets its an iterator is not callable in this context before ``toSeq`` gets its

View file

@ -42,7 +42,7 @@ List of warnings
---------------- ----------------
Each warning can be activated individually with ``--warning[NAME]:on|off`` or Each warning can be activated individually with ``--warning[NAME]:on|off`` or
in a ``push`` pragma. in a ``push`` pragma.
========================== ============================================ ========================== ============================================
Name Description Name Description

View file

@ -1013,17 +1013,16 @@ at runtime by 0, the second by 1 and so on. Example:
.. code-block:: nim .. code-block:: nim
type type
TDirection = enum Direction = enum
north, east, south, west north, east, south, west
var x = south # `x` is of type `TDirection`; its value is `south` var x = south # `x` is of type `Direction`; its value is `south`
echo($x) # writes "south" to `stdout` echo($x) # writes "south" to `stdout`
(To prefix a new type with the letter ``T`` is a convention in Nim.)
All comparison operators can be used with enumeration types. All comparison operators can be used with enumeration types.
An enumeration's symbol can be qualified to avoid ambiguities: An enumeration's symbol can be qualified to avoid ambiguities:
``TDirection.south``. ``Direction.south``.
The ``$`` operator can convert any enumeration value to its name, the ``ord`` The ``$`` operator can convert any enumeration value to its name, the ``ord``
proc to its underlying integer value. proc to its underlying integer value.
@ -1037,7 +1036,7 @@ An explicit ordered enum can have *holes*:
.. code-block:: nim .. code-block:: nim
type type
TMyEnum = enum MyEnum = enum
a = 2, b = 4, c = 89 a = 2, b = 4, c = 89
@ -1075,11 +1074,11 @@ A subrange type is a range of values from an integer or enumeration type
.. code-block:: nim .. code-block:: nim
type type
TSubrange = range[0..5] Subrange = range[0..5]
``TSubrange`` is a subrange of ``int`` which can only hold the values 0 ``Subrange`` is a subrange of ``int`` which can only hold the values 0
to 5. Assigning any other value to a variable of type ``TSubrange`` is a to 5. Assigning any other value to a variable of type ``Subrange`` is a
compile-time or runtime error. Assignments from the base type to one of its compile-time or runtime error. Assignments from the base type to one of its
subrange types (and vice versa) are allowed. subrange types (and vice versa) are allowed.
@ -1106,9 +1105,9 @@ Arrays can be constructed via ``[]``:
.. code-block:: nim .. code-block:: nim
type type
TIntArray = array[0..5, int] # an array that is indexed with 0..5 IntArray = array[0..5, int] # an array that is indexed with 0..5
var var
x: TIntArray x: IntArray
x = [1, 2, 3, 4, 5, 6] x = [1, 2, 3, 4, 5, 6]
for i in low(x)..high(x): for i in low(x)..high(x):
echo(x[i]) echo(x[i])
@ -1127,13 +1126,13 @@ array `a` and `high(a) <system.html#high>`_ the highest valid index.
.. code-block:: nim .. code-block:: nim
type type
TDirection = enum Direction = enum
north, east, south, west north, east, south, west
TBlinkLights = enum BlinkLights = enum
off, on, slowBlink, mediumBlink, fastBlink off, on, slowBlink, mediumBlink, fastBlink
TLevelSetting = array[north..west, TBlinkLights] LevelSetting = array[north..west, BlinkLights]
var var
level : TLevelSetting level: LevelSetting
level[north] = on level[north] = on
level[south] = slowBlink level[south] = slowBlink
level[east] = fastBlink level[east] = fastBlink
@ -1152,9 +1151,9 @@ subdivided in height levels accessed through their integer index:
.. code-block:: nim .. code-block:: nim
type type
TLightTower = array[1..10, TLevelSetting] LightTower = array[1..10, LevelSetting]
var var
tower: TLightTower tower: LightTower
tower[1][north] = slowBlink tower[1][north] = slowBlink
tower[1][east] = mediumBlink tower[1][east] = mediumBlink
echo len(tower) # --> 10 echo len(tower) # --> 10
@ -1165,24 +1164,24 @@ subdivided in height levels accessed through their integer index:
#tower[0][1] = on #tower[0][1] = on
Note how the built-in ``len`` proc returns only the array's first dimension Note how the built-in ``len`` proc returns only the array's first dimension
length. Another way of defining the ``TLightTower`` to show better its length. Another way of defining the ``LightTower`` to show better its
nested nature would be to omit the previous definition of the ``TLevelSetting`` nested nature would be to omit the previous definition of the ``LevelSetting``
type and instead write it embedded directly as the type of the first dimension: type and instead write it embedded directly as the type of the first dimension:
.. code-block:: nim .. code-block:: nim
type type
TLightTower = array[1..10, array[north..west, TBlinkLights]] LightTower = array[1..10, array[north..west, BlinkLights]]
It is quite frequent to have arrays start at zero, so there's a shortcut syntax It is quite frequent to have arrays start at zero, so there's a shortcut syntax
to specify a range from zero to the specified index minus one: to specify a range from zero to the specified index minus one:
.. code-block:: nim .. code-block:: nim
type type
TIntArray = array[0..5, int] # an array that is indexed with 0..5 IntArray = array[0..5, int] # an array that is indexed with 0..5
TQuickArray = array[6, int] # an array that is indexed with 0..5 QuickArray = array[6, int] # an array that is indexed with 0..5
var var
x: TIntArray x: IntArray
y: TQuickArray y: QuickArray
x = [1, 2, 3, 4, 5, 6] x = [1, 2, 3, 4, 5, 6]
y = x y = x
for i in low(x)..high(x): for i in low(x)..high(x):
@ -1436,16 +1435,16 @@ operators perform implicit dereferencing operations for reference types:
.. code-block:: nim .. code-block:: nim
type type
PNode = ref TNode Node = ref NodeObj
TNode = tuple[le, ri: PNode, data: int] NodeObj = object
le, ri: PNode
data: int
var var
n: PNode n: Node
new(n) new(n)
n.data = 9 n.data = 9
# no need to write n[].data; in fact n[].data is highly discouraged! # no need to write n[].data; in fact n[].data is highly discouraged!
(As a convention, reference types use a 'P' prefix.)
To allocate a new traced object, the built-in procedure ``new`` has to be used. To allocate a new traced object, the built-in procedure ``new`` has to be used.
To deal with untraced memory, the procedures ``alloc``, ``dealloc`` and To deal with untraced memory, the procedures ``alloc``, ``dealloc`` and
``realloc`` can be used. The documentation of the `system <system.html>`_ ``realloc`` can be used. The documentation of the `system <system.html>`_
@ -1573,11 +1572,11 @@ rules apply:
.. code-block:: nim .. code-block:: nim
# Module A # Module A
proc x*(a: int): string = result = $a proc x*(a: int): string = $a
.. code-block:: nim .. code-block:: nim
# Module B # Module B
proc x*(a: string): string = result = $a proc x*(a: string): string = $a
.. code-block:: nim .. code-block:: nim
# Module C # Module C

View file

@ -11,8 +11,7 @@ Nim Tutorial (Part II)
Introduction Introduction
============ ============
"Object-oriented programming is an exceptionally bad idea which could "Repetition renders the ridiculous reasonable." -- Norman Wildberger
only have originated in California." --Edsger Dijkstra
This document is a tutorial for the advanced constructs of the *Nim* This document is a tutorial for the advanced constructs of the *Nim*

View file

@ -51,20 +51,20 @@ proc initEventHandler*(name: string): EventHandler =
result.handlers = @[] result.handlers = @[]
result.name = name result.name = name
proc addHandler*(handler: var EventHandler, func: proc(e: EventArgs) {.closure.}) = proc addHandler*(handler: var EventHandler, fn: proc(e: EventArgs) {.closure.}) =
## Adds the callback to the specified event handler. ## Adds the callback to the specified event handler.
handler.handlers.add(func) handler.handlers.add(fn)
proc removeHandler*(handler: var EventHandler, func: proc(e: EventArgs) {.closure.}) = proc removeHandler*(handler: var EventHandler, fn: proc(e: EventArgs) {.closure.}) =
## Removes the callback from the specified event handler. ## Removes the callback from the specified event handler.
for i in countup(0, len(handler.handlers) -1): for i in countup(0, len(handler.handlers) -1):
if func == handler.handlers[i]: if fn == handler.handlers[i]:
handler.handlers.del(i) handler.handlers.del(i)
break break
proc containsHandler*(handler: var EventHandler, func: proc(e: EventArgs) {.closure.}): bool = proc containsHandler*(handler: var EventHandler, fn: proc(e: EventArgs) {.closure.}): bool =
## Checks if a callback is registered to this event handler. ## Checks if a callback is registered to this event handler.
return handler.handlers.contains(func) return handler.handlers.contains(fn)
proc clearHandlers*(handler: var EventHandler) = proc clearHandlers*(handler: var EventHandler) =
@ -76,21 +76,21 @@ proc getEventHandler(emitter: var EventEmitter, event: string): int =
if emitter.s[k].name == event: return k if emitter.s[k].name == event: return k
return -1 return -1
proc on*(emitter: var EventEmitter, event: string, func: proc(e: EventArgs) {.closure.}) = proc on*(emitter: var EventEmitter, event: string, fn: proc(e: EventArgs) {.closure.}) =
## Assigns a event handler with the specified callback. If the event ## Assigns a event handler with the specified callback. If the event
## doesn't exist, it will be created. ## doesn't exist, it will be created.
var i = getEventHandler(emitter, event) var i = getEventHandler(emitter, event)
if i < 0: if i < 0:
var eh = initEventHandler(event) var eh = initEventHandler(event)
addHandler(eh, func) addHandler(eh, fn)
emitter.s.add(eh) emitter.s.add(eh)
else: else:
addHandler(emitter.s[i], func) addHandler(emitter.s[i], fn)
proc emit*(emitter: var EventEmitter, eventhandler: var EventHandler, proc emit*(emitter: var EventEmitter, eventhandler: var EventHandler,
args: EventArgs) = args: EventArgs) =
## Fires an event handler with specified event arguments. ## Fires an event handler with specified event arguments.
for func in items(eventhandler.handlers): func(args) for fn in items(eventhandler.handlers): fn(args)
proc emit*(emitter: var EventEmitter, event: string, args: EventArgs) = proc emit*(emitter: var EventEmitter, event: string, args: EventArgs) =
## Fires an event handler with specified event arguments. ## Fires an event handler with specified event arguments.