promote std prefix in docs (#17128)
This commit is contained in:
parent
778f124af0
commit
69a78c9183
9 changed files with 41 additions and 41 deletions
|
|
@ -106,7 +106,7 @@ sample.nim:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
## This module is a sample.
|
## This module is a sample.
|
||||||
|
|
||||||
import strutils
|
import std/strutils
|
||||||
|
|
||||||
proc helloWorld*(times: int) =
|
proc helloWorld*(times: int) =
|
||||||
## Takes an integer and outputs
|
## Takes an integer and outputs
|
||||||
|
|
|
||||||
|
|
@ -258,7 +258,7 @@ as a "shallow" tree. Let's assume we compile module ``m`` with the
|
||||||
following contents:
|
following contents:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
import strutils
|
import std/strutils
|
||||||
|
|
||||||
var x*: int = 90
|
var x*: int = 90
|
||||||
{.compile: "foo.c".}
|
{.compile: "foo.c".}
|
||||||
|
|
@ -270,7 +270,7 @@ following contents:
|
||||||
Conceptually this is the AST we store for the module:
|
Conceptually this is the AST we store for the module:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
import strutils
|
import std/strutils
|
||||||
|
|
||||||
var x*
|
var x*
|
||||||
{.compile: "foo.c".}
|
{.compile: "foo.c".}
|
||||||
|
|
|
||||||
|
|
@ -830,7 +830,7 @@ problem!)
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
:test: "nim c $1"
|
:test: "nim c $1"
|
||||||
import strformat
|
import std/strformat
|
||||||
|
|
||||||
var fib_n {.compileTime.}: int
|
var fib_n {.compileTime.}: int
|
||||||
var fib_prev {.compileTime.}: int
|
var fib_prev {.compileTime.}: int
|
||||||
|
|
@ -2113,7 +2113,7 @@ modeled as a string. However, using string templates and filling in the
|
||||||
values is vulnerable to the famous `SQL injection attack`:idx:\:
|
values is vulnerable to the famous `SQL injection attack`:idx:\:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
import strutils
|
import std/strutils
|
||||||
|
|
||||||
proc query(db: DbHandle, statement: string) = ...
|
proc query(db: DbHandle, statement: string) = ...
|
||||||
|
|
||||||
|
|
@ -2145,7 +2145,7 @@ subtype relation between the abstract type and its base type. Explicit type
|
||||||
conversions from ``string`` to ``SQL`` are allowed:
|
conversions from ``string`` to ``SQL`` are allowed:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
import strutils, sequtils
|
import std/[strutils, sequtils]
|
||||||
|
|
||||||
proc properQuote(s: string): SQL =
|
proc properQuote(s: string): SQL =
|
||||||
# quotes a string properly for an SQL statement
|
# quotes a string properly for an SQL statement
|
||||||
|
|
@ -2791,7 +2791,7 @@ Const section
|
||||||
A const section declares constants whose values are constant expressions:
|
A const section declares constants whose values are constant expressions:
|
||||||
|
|
||||||
.. code-block::
|
.. code-block::
|
||||||
import strutils
|
import std/[strutils]
|
||||||
const
|
const
|
||||||
roundPi = 3.1415
|
roundPi = 3.1415
|
||||||
constEval = contains("abc", 'b') # computed at compile time!
|
constEval = contains("abc", 'b') # computed at compile time!
|
||||||
|
|
@ -4193,7 +4193,7 @@ parameters of an outer factory proc:
|
||||||
The call can be made more like an inline iterator with a for loop macro:
|
The call can be made more like an inline iterator with a for loop macro:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
import macros
|
import std/macros
|
||||||
macro toItr(x: ForLoopStmt): untyped =
|
macro toItr(x: ForLoopStmt): untyped =
|
||||||
let expr = x[0]
|
let expr = x[0]
|
||||||
let call = x[1][1] # Get foo out of toItr(foo)
|
let call = x[1][1] # Get foo out of toItr(foo)
|
||||||
|
|
@ -4327,7 +4327,7 @@ needs to fit the types of ``except`` branches, but the type of the ``finally``
|
||||||
branch always has to be ``void``:
|
branch always has to be ``void``:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
from strutils import parseInt
|
from std/strutils import parseInt
|
||||||
|
|
||||||
let x = try: parseInt("133a")
|
let x = try: parseInt("133a")
|
||||||
except: -1
|
except: -1
|
||||||
|
|
@ -5423,7 +5423,7 @@ Another common example is this:
|
||||||
:test: "nim c $1"
|
:test: "nim c $1"
|
||||||
:status: 1
|
:status: 1
|
||||||
|
|
||||||
from sequtils import toSeq
|
from std/sequtils import toSeq
|
||||||
|
|
||||||
iterator something: string =
|
iterator something: string =
|
||||||
yield "Hello"
|
yield "Hello"
|
||||||
|
|
@ -5443,7 +5443,7 @@ binds to symbols prohibits this.
|
||||||
:test: "nim c $1"
|
:test: "nim c $1"
|
||||||
:status: 1
|
:status: 1
|
||||||
|
|
||||||
import sequtils
|
import std/sequtils
|
||||||
|
|
||||||
var myItems = @[1,3,3,7]
|
var myItems = @[1,3,3,7]
|
||||||
let N1 = count(myItems, 3) # OK
|
let N1 = count(myItems, 3) # OK
|
||||||
|
|
@ -5489,7 +5489,7 @@ variable number of arguments:
|
||||||
|
|
||||||
# to work with Nim syntax trees, we need an API that is defined in the
|
# to work with Nim syntax trees, we need an API that is defined in the
|
||||||
# ``macros`` module:
|
# ``macros`` module:
|
||||||
import macros
|
import std/macros
|
||||||
|
|
||||||
macro debug(args: varargs[untyped]): untyped =
|
macro debug(args: varargs[untyped]): untyped =
|
||||||
# `args` is a collection of `NimNode` values that each contain the
|
# `args` is a collection of `NimNode` values that each contain the
|
||||||
|
|
@ -5548,7 +5548,7 @@ builtin can be used for that:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
:test: "nim c $1"
|
:test: "nim c $1"
|
||||||
|
|
||||||
import macros
|
import std/macros
|
||||||
|
|
||||||
macro debug(n: varargs[typed]): untyped =
|
macro debug(n: varargs[typed]): untyped =
|
||||||
result = newNimNode(nnkStmtList, n)
|
result = newNimNode(nnkStmtList, n)
|
||||||
|
|
@ -5596,7 +5596,7 @@ statement. The following example should show how this feature could be
|
||||||
used for a lexical analyzer.
|
used for a lexical analyzer.
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
import macros
|
import std/macros
|
||||||
|
|
||||||
macro case_token(args: varargs[untyped]): untyped =
|
macro case_token(args: varargs[untyped]): untyped =
|
||||||
echo args.treeRepr
|
echo args.treeRepr
|
||||||
|
|
@ -5633,7 +5633,7 @@ type ``system.ForLoopStmt`` can rewrite the entirety of a ``for`` loop:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
:test: "nim c $1"
|
:test: "nim c $1"
|
||||||
|
|
||||||
import macros
|
import std/macros
|
||||||
|
|
||||||
macro enumerate(x: ForLoopStmt): untyped =
|
macro enumerate(x: ForLoopStmt): untyped =
|
||||||
expectKind x, nnkForStmt
|
expectKind x, nnkForStmt
|
||||||
|
|
@ -5716,7 +5716,7 @@ One can force an expression to be evaluated at compile time as a constant
|
||||||
expression by coercing it to a corresponding ``static`` type:
|
expression by coercing it to a corresponding ``static`` type:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
import math
|
import std/math
|
||||||
|
|
||||||
echo static(fac(5)), " ", static[bool](16.isPowerOfTwo)
|
echo static(fac(5)), " ", static[bool](16.isPowerOfTwo)
|
||||||
|
|
||||||
|
|
@ -5796,7 +5796,7 @@ simply passed as a ``NimNode`` to the macro, like everything else.
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
||||||
import macros
|
import std/macros
|
||||||
|
|
||||||
macro forwardType(arg: typedesc): typedesc =
|
macro forwardType(arg: typedesc): typedesc =
|
||||||
# ``arg`` is of type ``NimNode``
|
# ``arg`` is of type ``NimNode``
|
||||||
|
|
@ -6214,7 +6214,7 @@ but accessed at runtime:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
:test: "nim c -r $1"
|
:test: "nim c -r $1"
|
||||||
|
|
||||||
import macros
|
import std/macros
|
||||||
|
|
||||||
var nameToProc {.compileTime.}: seq[(string, proc (): string {.nimcall.})]
|
var nameToProc {.compileTime.}: seq[(string, proc (): string {.nimcall.})]
|
||||||
|
|
||||||
|
|
@ -6607,7 +6607,7 @@ is uncertain (it may be removed at any time).
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
import threadpool
|
import std/threadpool
|
||||||
{.experimental: "parallel".}
|
{.experimental: "parallel".}
|
||||||
|
|
||||||
proc threadedEcho(s: string, i: int) =
|
proc threadedEcho(s: string, i: int) =
|
||||||
|
|
@ -7587,7 +7587,7 @@ The ``dynlib`` pragma supports not only constant strings as an argument but also
|
||||||
string expressions in general:
|
string expressions in general:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
import os
|
import std/os
|
||||||
|
|
||||||
proc getDllName: string =
|
proc getDllName: string =
|
||||||
result = "mylib.dll"
|
result = "mylib.dll"
|
||||||
|
|
|
||||||
|
|
@ -606,7 +606,7 @@ The concept types can be parametric just like the regular generic types:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
### matrixalgo.nim
|
### matrixalgo.nim
|
||||||
|
|
||||||
import typetraits
|
import std/typetraits
|
||||||
|
|
||||||
type
|
type
|
||||||
AnyMatrix*[R, C: static int; T] = concept m, var mvar, type M
|
AnyMatrix*[R, C: static int; T] = concept m, var mvar, type M
|
||||||
|
|
@ -743,7 +743,7 @@ type is an instance of it:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
:test: "nim c $1"
|
:test: "nim c $1"
|
||||||
|
|
||||||
import sugar, typetraits
|
import std/[sugar, typetraits]
|
||||||
|
|
||||||
type
|
type
|
||||||
Functor[A] = concept f
|
Functor[A] = concept f
|
||||||
|
|
@ -761,7 +761,7 @@ type is an instance of it:
|
||||||
# the Functor to a instance of a different type, given
|
# the Functor to a instance of a different type, given
|
||||||
# a suitable `map` operation for the enclosed values
|
# a suitable `map` operation for the enclosed values
|
||||||
|
|
||||||
import options
|
import std/options
|
||||||
echo Option[int] is Functor # prints true
|
echo Option[int] is Functor # prints true
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -989,7 +989,7 @@ equality operator for tuples (as provided in ``system.==``):
|
||||||
|
|
||||||
{.experimental: "caseStmtMacros".}
|
{.experimental: "caseStmtMacros".}
|
||||||
|
|
||||||
import macros
|
import std/macros
|
||||||
|
|
||||||
macro `case`(n: tuple): untyped =
|
macro `case`(n: tuple): untyped =
|
||||||
result = newTree(nnkIfStmt)
|
result = newTree(nnkIfStmt)
|
||||||
|
|
@ -1259,7 +1259,7 @@ The ``**`` is much like the ``*`` operator, except that it gathers not only
|
||||||
all the arguments, but also the matched operators in reverse polish notation:
|
all the arguments, but also the matched operators in reverse polish notation:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
import macros
|
import std/macros
|
||||||
|
|
||||||
type
|
type
|
||||||
Matrix = object
|
Matrix = object
|
||||||
|
|
@ -1331,7 +1331,7 @@ Example: Hoisting
|
||||||
The following example shows how some form of hoisting can be implemented:
|
The following example shows how some form of hoisting can be implemented:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
import pegs
|
import std/pegs
|
||||||
|
|
||||||
template optPeg{peg(pattern)}(pattern: string{lit}): Peg =
|
template optPeg{peg(pattern)}(pattern: string{lit}): Peg =
|
||||||
var gl {.global, gensym.} = peg(pattern)
|
var gl {.global, gensym.} = peg(pattern)
|
||||||
|
|
@ -1405,7 +1405,7 @@ Spawn statement
|
||||||
`spawn`:idx: can be used to pass a task to the thread pool:
|
`spawn`:idx: can be used to pass a task to the thread pool:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
import threadpool
|
import std/threadpool
|
||||||
|
|
||||||
proc processLine(line: string) =
|
proc processLine(line: string) =
|
||||||
discard "do some heavy lifting here"
|
discard "do some heavy lifting here"
|
||||||
|
|
@ -1437,7 +1437,7 @@ with the ``^`` operator is **blocking**. However, one can use ``blockUntilAny``
|
||||||
wait on multiple flow variables at the same time:
|
wait on multiple flow variables at the same time:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
import threadpool, ...
|
import std/threadpool, ...
|
||||||
|
|
||||||
# wait until 2 out of 3 servers received the update:
|
# wait until 2 out of 3 servers received the update:
|
||||||
proc main =
|
proc main =
|
||||||
|
|
@ -1466,7 +1466,7 @@ Example:
|
||||||
:test: "nim c --threads:on $1"
|
:test: "nim c --threads:on $1"
|
||||||
|
|
||||||
# Compute PI in an inefficient way
|
# Compute PI in an inefficient way
|
||||||
import strutils, math, threadpool
|
import std/[strutils, math, threadpool]
|
||||||
{.experimental: "parallel".}
|
{.experimental: "parallel".}
|
||||||
|
|
||||||
proc term(k: float): float = 4 * math.pow(-1, k) / (2*k + 1)
|
proc term(k: float): float = 4 * math.pow(-1, k) / (2*k + 1)
|
||||||
|
|
|
||||||
|
|
@ -228,7 +228,7 @@ See the following (incomplete) example:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
||||||
import distros
|
import std/distros
|
||||||
|
|
||||||
# Architectures.
|
# Architectures.
|
||||||
if defined(amd64):
|
if defined(amd64):
|
||||||
|
|
|
||||||
|
|
@ -227,7 +227,7 @@ JavaScript tests:
|
||||||
targets: "js"
|
targets: "js"
|
||||||
"""
|
"""
|
||||||
when defined(js):
|
when defined(js):
|
||||||
import jsconsole
|
import std/jsconsole
|
||||||
console.log("My Frontend Project")
|
console.log("My Frontend Project")
|
||||||
|
|
||||||
Compile-time tests:
|
Compile-time tests:
|
||||||
|
|
|
||||||
|
|
@ -310,7 +310,7 @@ For integers or other ordinal types value ranges are also possible:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
# this statement will be explained later:
|
# this statement will be explained later:
|
||||||
from strutils import parseInt
|
from std/strutils import parseInt
|
||||||
|
|
||||||
echo "A number please: "
|
echo "A number please: "
|
||||||
let n = parseInt(readLine(stdin))
|
let n = parseInt(readLine(stdin))
|
||||||
|
|
@ -1607,7 +1607,7 @@ variables! For example:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
:test: "nim c $1"
|
:test: "nim c $1"
|
||||||
|
|
||||||
import os
|
import std/os
|
||||||
|
|
||||||
let
|
let
|
||||||
path = "usr/local/nimc.html"
|
path = "usr/local/nimc.html"
|
||||||
|
|
|
||||||
|
|
@ -182,7 +182,7 @@ for any type:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
:test: "nim c $1"
|
:test: "nim c $1"
|
||||||
import strutils
|
import std/strutils
|
||||||
|
|
||||||
echo "abc".len # is the same as echo len("abc")
|
echo "abc".len # is the same as echo len("abc")
|
||||||
echo "abc".toUpperAscii()
|
echo "abc".toUpperAscii()
|
||||||
|
|
@ -196,7 +196,7 @@ So "pure object oriented" code is easy to write:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
:test: "nim c $1"
|
:test: "nim c $1"
|
||||||
import strutils, sequtils
|
import std/[strutils, sequtils]
|
||||||
|
|
||||||
stdout.writeLine("Give a list of numbers (separated by spaces): ")
|
stdout.writeLine("Give a list of numbers (separated by spaces): ")
|
||||||
stdout.write(stdin.readLine.splitWhitespace.map(parseInt).max.`$`)
|
stdout.write(stdin.readLine.splitWhitespace.map(parseInt).max.`$`)
|
||||||
|
|
@ -375,7 +375,7 @@ The ``try`` statement handles exceptions:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
:test: "nim c $1"
|
:test: "nim c $1"
|
||||||
from strutils import parseInt
|
from std/strutils import parseInt
|
||||||
|
|
||||||
# read the first two lines of a text file that should contain numbers
|
# read the first two lines of a text file that should contain numbers
|
||||||
# and tries to add them
|
# and tries to add them
|
||||||
|
|
@ -649,7 +649,7 @@ Example: Lifting Procs
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
:test: "nim c $1"
|
:test: "nim c $1"
|
||||||
import math
|
import std/math
|
||||||
|
|
||||||
template liftScalarProc(fname) =
|
template liftScalarProc(fname) =
|
||||||
## Lift a proc taking one scalar parameter and returning a
|
## Lift a proc taking one scalar parameter and returning a
|
||||||
|
|
|
||||||
|
|
@ -91,7 +91,7 @@ but in the macro body ``arg`` is just like a normal parameter of type
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
||||||
import macros
|
import std/macros
|
||||||
|
|
||||||
macro myMacro(arg: static[int]): untyped =
|
macro myMacro(arg: static[int]): untyped =
|
||||||
echo arg # just an int (7), not ``NimNode``
|
echo arg # just an int (7), not ``NimNode``
|
||||||
|
|
@ -193,7 +193,7 @@ them into the tree.
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
:test: "nim c $1"
|
:test: "nim c $1"
|
||||||
|
|
||||||
import macros
|
import std/macros
|
||||||
|
|
||||||
type
|
type
|
||||||
MyType = object
|
MyType = object
|
||||||
|
|
@ -232,7 +232,7 @@ correct argument should look like.
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
:test: "nim c $1"
|
:test: "nim c $1"
|
||||||
|
|
||||||
import macros
|
import std/macros
|
||||||
|
|
||||||
macro myAssert(arg: untyped): untyped =
|
macro myAssert(arg: untyped): untyped =
|
||||||
echo arg.treeRepr
|
echo arg.treeRepr
|
||||||
|
|
@ -258,7 +258,7 @@ written.
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
:test: "nim c $1"
|
:test: "nim c $1"
|
||||||
|
|
||||||
import macros
|
import std/macros
|
||||||
|
|
||||||
macro myAssert(arg: untyped): untyped =
|
macro myAssert(arg: untyped): untyped =
|
||||||
# all node kind identifiers are prefixed with "nnk"
|
# all node kind identifiers are prefixed with "nnk"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue