mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-03 10:24:44 +00:00
minor improvements to the docs
This commit is contained in:
@@ -33,7 +33,7 @@ import
|
||||
ast, astalgo, strutils, hashes, trees, platform, magicsys, extccomp,
|
||||
options, nversion, nimsets, msgs, crc, bitsets, idents, lists, types, os,
|
||||
times, ropes, math, passes, ccgutils, wordrecg, renderer, rodread, rodutils,
|
||||
intsets, cgmeth
|
||||
intsets, cgmeth, lowerings
|
||||
|
||||
type
|
||||
TTarget = enum
|
||||
@@ -1211,13 +1211,17 @@ proc genVarInit(p: PProc, v: PSym, n: PNode) =
|
||||
proc genVarStmt(p: PProc, n: PNode) =
|
||||
for i in countup(0, sonsLen(n) - 1):
|
||||
var a = n.sons[i]
|
||||
if a.kind == nkCommentStmt: continue
|
||||
assert(a.kind == nkIdentDefs)
|
||||
assert(a.sons[0].kind == nkSym)
|
||||
var v = a.sons[0].sym
|
||||
if lfNoDecl in v.loc.flags: continue
|
||||
genLineDir(p, a)
|
||||
genVarInit(p, v, a.sons[2])
|
||||
if a.kind != nkCommentStmt:
|
||||
if a.kind == nkVarTuple:
|
||||
let unpacked = lowerTupleUnpacking(a, p.prc)
|
||||
genStmt(p, unpacked)
|
||||
else:
|
||||
assert(a.kind == nkIdentDefs)
|
||||
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) =
|
||||
if lfNoDecl notin c.loc.flags and not p.g.generatedSyms.containsOrIncl(c.id):
|
||||
|
||||
@@ -250,7 +250,7 @@ Another common example is this:
|
||||
yield "Hello"
|
||||
yield "World"
|
||||
|
||||
var info = something().toSeq()
|
||||
var info = toSeq(something())
|
||||
|
||||
The problem here is that the compiler already decided that ``something()`` as
|
||||
an iterator is not callable in this context before ``toSeq`` gets its
|
||||
|
||||
@@ -42,7 +42,7 @@ List of warnings
|
||||
----------------
|
||||
|
||||
Each warning can be activated individually with ``--warning[NAME]:on|off`` or
|
||||
in a ``push`` pragma.
|
||||
in a ``push`` pragma.
|
||||
|
||||
========================== ============================================
|
||||
Name Description
|
||||
|
||||
59
doc/tut1.txt
59
doc/tut1.txt
@@ -1013,17 +1013,16 @@ at runtime by 0, the second by 1 and so on. Example:
|
||||
.. code-block:: nim
|
||||
|
||||
type
|
||||
TDirection = enum
|
||||
Direction = enum
|
||||
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`
|
||||
|
||||
(To prefix a new type with the letter ``T`` is a convention in Nim.)
|
||||
All comparison operators can be used with enumeration types.
|
||||
|
||||
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``
|
||||
proc to its underlying integer value.
|
||||
@@ -1037,7 +1036,7 @@ An explicit ordered enum can have *holes*:
|
||||
|
||||
.. code-block:: nim
|
||||
type
|
||||
TMyEnum = enum
|
||||
MyEnum = enum
|
||||
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
|
||||
type
|
||||
TSubrange = range[0..5]
|
||||
Subrange = range[0..5]
|
||||
|
||||
|
||||
``TSubrange`` 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
|
||||
``Subrange`` is a subrange of ``int`` which can only hold the values 0
|
||||
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
|
||||
subrange types (and vice versa) are allowed.
|
||||
|
||||
@@ -1106,9 +1105,9 @@ Arrays can be constructed via ``[]``:
|
||||
.. code-block:: nim
|
||||
|
||||
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
|
||||
x: TIntArray
|
||||
x: IntArray
|
||||
x = [1, 2, 3, 4, 5, 6]
|
||||
for i in low(x)..high(x):
|
||||
echo(x[i])
|
||||
@@ -1127,13 +1126,13 @@ array `a` and `high(a) <system.html#high>`_ the highest valid index.
|
||||
|
||||
.. code-block:: nim
|
||||
type
|
||||
TDirection = enum
|
||||
Direction = enum
|
||||
north, east, south, west
|
||||
TBlinkLights = enum
|
||||
BlinkLights = enum
|
||||
off, on, slowBlink, mediumBlink, fastBlink
|
||||
TLevelSetting = array[north..west, TBlinkLights]
|
||||
LevelSetting = array[north..west, BlinkLights]
|
||||
var
|
||||
level : TLevelSetting
|
||||
level: LevelSetting
|
||||
level[north] = on
|
||||
level[south] = slowBlink
|
||||
level[east] = fastBlink
|
||||
@@ -1152,9 +1151,9 @@ subdivided in height levels accessed through their integer index:
|
||||
|
||||
.. code-block:: nim
|
||||
type
|
||||
TLightTower = array[1..10, TLevelSetting]
|
||||
LightTower = array[1..10, LevelSetting]
|
||||
var
|
||||
tower: TLightTower
|
||||
tower: LightTower
|
||||
tower[1][north] = slowBlink
|
||||
tower[1][east] = mediumBlink
|
||||
echo len(tower) # --> 10
|
||||
@@ -1165,24 +1164,24 @@ subdivided in height levels accessed through their integer index:
|
||||
#tower[0][1] = on
|
||||
|
||||
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
|
||||
nested nature would be to omit the previous definition of the ``TLevelSetting``
|
||||
length. Another way of defining the ``LightTower`` to show better its
|
||||
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:
|
||||
|
||||
.. code-block:: nim
|
||||
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
|
||||
to specify a range from zero to the specified index minus one:
|
||||
|
||||
.. code-block:: nim
|
||||
type
|
||||
TIntArray = array[0..5, int] # an array that is indexed with 0..5
|
||||
TQuickArray = array[6, int] # an array that is indexed with 0..5
|
||||
IntArray = array[0..5, int] # an array that is indexed with 0..5
|
||||
QuickArray = array[6, int] # an array that is indexed with 0..5
|
||||
var
|
||||
x: TIntArray
|
||||
y: TQuickArray
|
||||
x: IntArray
|
||||
y: QuickArray
|
||||
x = [1, 2, 3, 4, 5, 6]
|
||||
y = x
|
||||
for i in low(x)..high(x):
|
||||
@@ -1436,16 +1435,16 @@ operators perform implicit dereferencing operations for reference types:
|
||||
.. code-block:: nim
|
||||
|
||||
type
|
||||
PNode = ref TNode
|
||||
TNode = tuple[le, ri: PNode, data: int]
|
||||
Node = ref NodeObj
|
||||
NodeObj = object
|
||||
le, ri: PNode
|
||||
data: int
|
||||
var
|
||||
n: PNode
|
||||
n: Node
|
||||
new(n)
|
||||
n.data = 9
|
||||
# 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 deal with untraced memory, the procedures ``alloc``, ``dealloc`` and
|
||||
``realloc`` can be used. The documentation of the `system <system.html>`_
|
||||
@@ -1573,11 +1572,11 @@ rules apply:
|
||||
|
||||
.. code-block:: nim
|
||||
# Module A
|
||||
proc x*(a: int): string = result = $a
|
||||
proc x*(a: int): string = $a
|
||||
|
||||
.. code-block:: nim
|
||||
# Module B
|
||||
proc x*(a: string): string = result = $a
|
||||
proc x*(a: string): string = $a
|
||||
|
||||
.. code-block:: nim
|
||||
# Module C
|
||||
|
||||
@@ -11,8 +11,7 @@ Nim Tutorial (Part II)
|
||||
Introduction
|
||||
============
|
||||
|
||||
"Object-oriented programming is an exceptionally bad idea which could
|
||||
only have originated in California." --Edsger Dijkstra
|
||||
"Repetition renders the ridiculous reasonable." -- Norman Wildberger
|
||||
|
||||
|
||||
This document is a tutorial for the advanced constructs of the *Nim*
|
||||
|
||||
@@ -51,20 +51,20 @@ proc initEventHandler*(name: string): EventHandler =
|
||||
result.handlers = @[]
|
||||
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.
|
||||
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.
|
||||
for i in countup(0, len(handler.handlers) -1):
|
||||
if func == handler.handlers[i]:
|
||||
if fn == handler.handlers[i]:
|
||||
handler.handlers.del(i)
|
||||
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.
|
||||
return handler.handlers.contains(func)
|
||||
return handler.handlers.contains(fn)
|
||||
|
||||
|
||||
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
|
||||
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
|
||||
## doesn't exist, it will be created.
|
||||
var i = getEventHandler(emitter, event)
|
||||
if i < 0:
|
||||
var eh = initEventHandler(event)
|
||||
addHandler(eh, func)
|
||||
addHandler(eh, fn)
|
||||
emitter.s.add(eh)
|
||||
else:
|
||||
addHandler(emitter.s[i], func)
|
||||
addHandler(emitter.s[i], fn)
|
||||
|
||||
proc emit*(emitter: var EventEmitter, eventhandler: var EventHandler,
|
||||
args: EventArgs) =
|
||||
## 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) =
|
||||
## Fires an event handler with specified event arguments.
|
||||
|
||||
Reference in New Issue
Block a user