On this page
system
Source EditThe compiler depends on the System module to work properly and the System module depends on the compiler. Most of the routines listed here use special compiler magic.
Each module implicitly imports the System module; it must not be listed explicitly. Because of this there cannot be a user-defined module named system
.
System module
The System module imports several separate modules, and their documentation is in separate files:
Here is a short overview of the most commonly used functions from the system
module. Function names in the tables below are clickable and will take you to the full documentation of the function.
There are many more functions available than the ones listed in this overview. Use the table of contents on the left-hand side and/or Ctrl+F
to navigate through this module.
Strings and characters
Proc | Usage |
---|---|
len(s) | Return the length of a string |
chr(i) | Convert an int in the range 0..255 to a character |
ord(c) | Return int value of a character |
a & b | Concatenate two strings |
s.add(c) | Add character to the string |
$ | Convert various types to string |
See also:
- strutils module for common string functions
- strformat module for string interpolation and formatting
- unicode module for Unicode UTF-8 handling
- strscans for
scanf
andscanp
macros, which offer easier substring extraction than regular expressions - strtabs module for efficient hash tables (dictionaries, in some programming languages) mapping from strings to strings
Seqs
Proc | Usage |
---|---|
newSeq | Create a new sequence of a given length |
newSeqOfCap | Create a new sequence with zero length and a given capacity |
setLen | Set the length of a sequence |
len | Return the length of a sequence |
@ | Turn an array into a sequence |
add | Add an item to the sequence |
insert | Insert an item at a specific position |
delete | Delete an item while preserving the order of elements (O(n) operation) |
del | O(1) removal, doesn't preserve the order |
pop | Remove and return last item of a sequence |
x & y | Concatenate two sequences |
x[a .. b] | Slice of a sequence (both ends included) |
x[a .. ^b] | Slice of a sequence but b is a reversed index (both ends included) |
x[a ..< b] | Slice of a sequence (excluded upper bound) |
See also:
- sequtils module for operations on container types (including strings)
- json module for a structure which allows heterogeneous members
- lists module for linked lists
Sets
Built-in bit sets.
Proc | Usage |
---|---|
incl | Include element y in the set x |
excl | Exclude element y from the set x |
card | Return the cardinality of the set, i.e. the number of elements |
a * b | Intersection |
a + b | Union |
a - b | Difference |
contains | Check if an element is in the set |
a < b | Check if a is a subset of b |
See also:
- setutils module for bit set convenience functions
- sets module for hash sets
- intsets module for efficient int sets
Numbers
Proc | Usage | Also known as (in other languages) |
---|---|---|
div | Integer division | // |
mod | Integer modulo (remainder) | % |
shl | Shift left | << |
shr | Shift right | >> |
ashr | Arithmetic shift right | |
and | Bitwise and |
& |
or | Bitwise or |
| |
xor | Bitwise xor |
^ |
not | Bitwise not (complement) |
~ |
toInt | Convert floating-point number into an int |
|
toFloat | Convert an integer into a float |
See also:
- math module for mathematical operations like trigonometric functions, logarithms, square and cubic roots, etc.
- complex module for operations on complex numbers
- rationals module for rational numbers
Ordinals
Ordinal type includes integer, bool, character, and enumeration types, as well as their subtypes.
Proc | Usage |
---|---|
succ | Successor of the value |
pred | Predecessor of the value |
inc | Increment the ordinal |
dec | Decrement the ordinal |
high | Return the highest possible value |
low | Return the lowest possible value |
ord | Return int value of an ordinal value |
Misc
Proc | Usage |
---|---|
is | Check if two arguments are of the same type |
isnot | Negated version of is |
!= | Not equals |
addr | Take the address of a memory location |
T and F | Boolean and |
T or F | Boolean or |
T xor F | Boolean xor (exclusive or) |
not T | Boolean not |
a[^x] | Take the element at the reversed index x |
a .. b | Binary slice that constructs an interval [a, b] |
a ..^ b | Interval [a, b] but b as reversed index |
a ..< b | Interval [a, b) (excluded upper bound) |
runnableExamples | Create testable documentation |
Channel support for threads.
Note: This is part of the system module. Do not import it directly. To activate thread support compile with the --threads:on
command line switch.
Note: Channels are designed for the Thread
type. They are unstable when used with spawn
Note: The current implementation of message passing does not work with cyclic data structures.
Note: Channels cannot be passed between threads. Use globals or pass them by ptr
.
Example
The following is a simple example of two different ways to use channels: blocking and non-blocking.
# Be sure to compile with --threads:on.
# The channels and threads modules are part of system and should not be
# imported.
import std/os
# Channels can either be:
# - declared at the module level, or
# - passed to procedures by ptr (raw pointer) -- see note on safety.
#
# For simplicity, in this example a channel is declared at module scope.
# Channels are generic, and they include support for passing objects between
# threads.
# Note that objects passed through channels will be deeply copied.
var chan: Channel[string]
# This proc will be run in another thread using the threads module.
proc firstWorker() =
chan.send("Hello World!")
# This is another proc to run in a background thread. This proc takes a while
# to send the message since it sleeps for 2 seconds (or 2000 milliseconds).
proc secondWorker() =
sleep(2000)
chan.send("Another message")
# Initialize the channel.
chan.open()
# Launch the worker.
var worker1: Thread[void]
createThread(worker1, firstWorker)
# Block until the message arrives, then print it out.
echo chan.recv() # "Hello World!"
# Wait for the thread to exit before moving on to the next example.
worker1.joinThread()
# Launch the other worker.
var worker2: Thread[void]
createThread(worker2, secondWorker)
# This time, use a non-blocking approach with tryRecv.
# Since the main thread is not blocked, it could be used to perform other
# useful work while it waits for data to arrive on the channel.
while true:
let tried = chan.tryRecv()
if tried.dataAvailable:
echo tried.msg # "Another message"
break
echo "Pretend I'm doing useful work..."
# For this example, sleep in order not to flood stdout with the above
# message.
sleep(400)
# Wait for the second thread to exit before cleaning up the channel.
worker2.joinThread()
# Clean up the channel.
chan.close()
Sample output
The program should output something similar to this, but keep in mind that exact results may vary in the real world:
Hello World!
Pretend I'm doing useful work...
Pretend I'm doing useful work...
Pretend I'm doing useful work...
Pretend I'm doing useful work...
Pretend I'm doing useful work...
Another message
Passing Channels Safely
Note that when passing objects to procedures on another thread by pointer (for example through a thread's argument), objects created using the default allocator will use thread-local, GC-managed memory. Thus it is generally safer to store channel objects in global variables (as in the above example), in which case they will use a process-wide (thread-safe) shared heap.
However, it is possible to manually allocate shared memory for channels using e.g. system.allocShared0
and pass these pointers through thread arguments:
proc worker(channel: ptr Channel[string]) =
let greeting = channel[].recv()
echo greeting
proc localChannelExample() =
# Use allocShared0 to allocate some shared-heap memory and zero it.
# The usual warnings about dealing with raw pointers apply. Exercise caution.
var channel = cast[ptr Channel[string]](
allocShared0(sizeof(Channel[string]))
)
channel[].open()
# Create a thread which will receive the channel as an argument.
var thread: Thread[ptr Channel[string]]
createThread(thread, worker, channel)
channel[].send("Hello from the main thread!")
# Clean up resources.
thread.joinThread()
channel[].close()
deallocShared(channel)
localChannelExample() # "Hello from the main thread!"
Imports
- exceptions, since, ctypes, ctypes, sysatomics, ansi_c, memory, syslocks, threadtypes, assertions, iterators, coro_detection, dollars, typedthreads, miscdollars, stacktraces, countbits_impl, syslocks, sysatomics, sharedlist, digitsutils, syslocks, digitsutils, widestrs, syncio
Types
-
any {....deprecated: "Deprecated since v1.5; Use auto instead.".} = distinct auto
-
auto
instead. See https://github.com/nim-lang/RFCs/issues/281 Source Edit
Deprecated; Use -
BackwardsIndex = distinct int
-
Type that is constructed by
^
for reversed array accesses. (See ^ template) Source Edit -
CatchableError = object of Exception
- Abstract class for all exceptions that are catchable. Source Edit
-
csize {.importc: "size_t", nodecl, ...deprecated: "use `csize_t` instead".} = int
-
size_t
in C. Don't use it. Source Edit
This isn't the same as -
Defect = object of Exception
-
Abstract base class for all exceptions that Nim's runtime raises but that are strictly uncatchable as they can also be mapped to a
quit
/trap
/exit
operation. Source Edit -
Endianness = enum littleEndian, bigEndian
- Type describing the endianness of a processor. Source Edit
-
Exception {.compilerproc, magic: "Exception".} = object of RootObj parent*: ref Exception ## Parent exception (can be used as a stack). name*: cstring ## The exception's name is its Nim identifier. ## This field is filled automatically in the ## `raise` statement. msg* {.exportc: "message".}: string ## The exception's message. Not ## providing an exception message ## is bad style. when defined(js): trace*: string else: trace*: seq[StackTraceEntry]
-
Base exception class.
Each exception has to inherit from
Source EditException
. See the full exception hierarchy. -
ForLoopStmt {.compilerproc.} = object
- A special type that marks a macro as a for-loop macro. See "For Loop Macro". Source Edit
-
GC_Strategy = enum gcThroughput, ## optimize for throughput gcResponsiveness, ## optimize for responsiveness (default) gcOptimizeTime, ## optimize for speed gcOptimizeSpace ## optimize for memory footprint
- The strategy the GC should use for the application. Source Edit
-
HSlice[T; U] = object a*: T ## The lower bound (inclusive). b*: U ## The upper bound (inclusive).
- "Heterogeneous" slice type. Source Edit
-
int {.magic: Int.}
- Default integer type; bitwidth depends on architecture, but is always the same as a pointer. Source Edit
-
Natural = range[0 .. high(int)]
-
is an
int
type ranging from zero to the maximum value of anint
. This type is often useful for documentation and debugging. Source Edit -
NimNode {.magic: "PNimrodNode".} = ref NimNodeObj
- Represents a Nim AST node. Macros operate on this type. Source Edit
-
openArray[T] {.magic: "OpenArray".}
- Generic type to construct open arrays. Open arrays are implemented as a pointer to the array data and a length field. Source Edit
-
Ordinal[T] {.magic: Ordinal.}
-
Generic ordinal type. Includes integer, bool, character, and enumeration types as well as their subtypes. See also
SomeOrdinal
. Source Edit -
owned[T] {.magic: "BuiltinType".}
-
type constructor to mark a ref/ptr or a closure as
owned
. Source Edit -
PFrame = ptr TFrame
- Represents a runtime frame of the call stack; part of the debugger API. Source Edit
-
pointer {.magic: Pointer.}
-
Built-in pointer type, use the
addr
operator to get a pointer to a variable. Source Edit -
Positive = range[1 .. high(int)]
-
is an
int
type ranging from one to the maximum value of anint
. This type is often useful for documentation and debugging. Source Edit -
RootEffect {.compilerproc.} = object of RootObj
-
Base effect class.
Each effect should inherit from
Source EditRootEffect
unless you know what you're doing. -
RootObj {.compilerproc, inheritable.} = object
-
The root of Nim's object hierarchy.
Objects should inherit from
Source EditRootObj
or one of its descendants. However, objects that have no ancestor are also allowed. -
SomeFloat = float | float32 | float64
- Type class matching all floating point number types. Source Edit
-
SomeOrdinal = int | int8 | int16 | int32 | int64 | bool | enum | uint | uint8 | uint16 | uint32 | uint64
-
Type class matching all ordinal types; however this includes enums with holes. See also
Ordinal
Source Edit -
SomeSignedInt = int | int8 | int16 | int32 | int64
- Type class matching all signed integer types. Source Edit
-
SomeUnsignedInt = uint | uint8 | uint16 | uint32 | uint64
- Type class matching all unsigned integer types. Source Edit
-
StackTraceEntry = object procname*: cstring ## Name of the proc that is currently executing. line*: int ## Line number of the proc that is currently executing. filename*: cstring ## Filename of the proc that is currently executing. when NimStackTraceMsgs: frameMsg*: string ## When a stacktrace is generated in a given frame and ## rendered at a later time, we should ensure the stacktrace ## data isn't invalidated; any pointer into PFrame is ## subject to being invalidated so shouldn't be stored. when defined(nimStackTraceOverride): programCounter*: uint ## Program counter - will be used to get the rest of the info, ## when `$` is called on this type. We can't use ## "cuintptr_t" in here. procnameStr*, filenameStr*: string ## GC-ed alternatives to "procname" and "filename"
-
In debug mode exceptions store the stack trace that led to them. A
StackTraceEntry
is a single entry of the stack trace. Source Edit -
static[T] {.magic: "Static".}
-
Meta type representing all values that can be evaluated at compile-time.
The type coercion
Source Editstatic(x)
can be used to force the compile-time evaluation of the given expressionx
. -
TFrame {.importc, nodecl, final.} = object prev*: PFrame ## Previous frame; used for chaining the call stack. procname*: cstring ## Name of the proc that is currently executing. line*: int ## Line number of the proc that is currently executing. filename*: cstring ## Filename of the proc that is currently executing. len*: int16 ## Length of the inspectable slots. calldepth*: int16 ## Used for max call depth checking. when NimStackTraceMsgs: frameMsgLen*: int ## end position in frameMsgBuf for this frame.
- The frame itself. Source Edit
-
type[T] {.magic: "Type".}
-
Meta type representing the type of all type values.
The coercion
Source Edittype(x)
can be used to obtain the type of the given expressionx
. -
typed {.magic: Stmt.}
- Meta type to denote an expression that is resolved (for templates). Source Edit
-
TypeOfMode = enum typeOfProc, ## Prefer the interpretation that means `x` is a proc call. typeOfIter ## Prefer the interpretation that means `x` is an iterator call.
-
Possible modes of
typeof
. Source Edit
Vars
-
errorMessageWriter: (proc (msg: string) {....tags: [WriteIOEffect], gcsafe, nimcall.})
-
Function that will be called instead of
stdmsg.write
when printing stacktrace. Unstable API. Source Edit -
globalRaiseHook: proc (e: ref Exception): bool {.nimcall, ...gcsafe.}
-
With this hook you can influence exception handling on a global level. If not nil, every 'raise' statement ends up calling this hook.
Warning: Ordinary application code should never set this hook! You better know what you do when setting this.
If
Source EditglobalRaiseHook
returns false, the exception is caught and does not propagate further through the call stack. -
localRaiseHook {.threadvar.}: proc (e: ref Exception): bool {.nimcall, ...gcsafe.}
-
With this hook you can influence exception handling on a thread local level. If not nil, every 'raise' statement ends up calling this hook.
Warning: Ordinary application code should never set this hook! You better know what you do when setting this.
If
Source EditlocalRaiseHook
returns false, the exception is caught and does not propagate further through the call stack. -
nimThreadDestructionHandlers {.threadvar.}: seq[ proc () {.closure, ...gcsafe, raises: [].}]
- Source Edit
-
onUnhandledException: (proc (errorMsg: string) {.nimcall, ...gcsafe.})
-
Set this error handler to override the existing behaviour on an unhandled exception.
The default is to write a stacktrace to
Source Editstderr
and then callquit(1)
. Unstable API. -
outOfMemHook: proc () {.nimcall, ...tags: [], gcsafe, raises: [].}
-
Set this variable to provide a procedure that should be called in case of an out of memory event. The standard handler writes an error message and terminates the program.
outOfMemHook
can be used to raise an exception in case of OOM like so:var gOutOfMem: ref EOutOfMemory new(gOutOfMem) # need to be allocated *before* OOM really happened! gOutOfMem.msg = "out of memory" proc handleOOM() = raise gOutOfMem system.outOfMemHook = handleOOM
If the handler does not raise an exception, ordinary control flow continues and the program is terminated.
Source Edit -
programResult {.compilerproc, exportc: "nim_program_result".}: int
-
deprecated, prefer
quit
orexitprocs.getProgramResult
,exitprocs.setProgramResult
. Source Edit -
unhandledExceptionHook: proc (e: ref Exception) {.nimcall, ...tags: [], gcsafe, raises: [].}
-
Set this variable to provide a procedure that should be called in case of an
unhandle exception
event. The standard handler writes an error message and terminates the program, except when using--os:any
Source Edit
Lets
Consts
-
appType {.magic: "AppType".}: string = ""
-
A string that describes the application type. Possible values:
"console"
,"gui"
,"lib"
. Source Edit -
CompileDate {.magic: "CompileDate".}: string = "0000-00-00"
-
The date (in UTC) of compilation as a string of the form
YYYY-MM-DD
. This works thanks to compiler magic. Source Edit -
CompileTime {.magic: "CompileTime".}: string = "00:00:00"
-
The time (in UTC) of compilation as a string of the form
HH:MM:SS
. This works thanks to compiler magic. Source Edit -
cpuEndian {.magic: "CpuEndian".}: Endianness = littleEndian
- The endianness of the target CPU. This is a valuable piece of information for low-level code only. This works thanks to compiler magic. Source Edit
-
hostCPU {.magic: "HostCPU".}: string = ""
-
A string that describes the host CPU.
Possible values:
Source Edit"i386"
,"alpha"
,"powerpc"
,"powerpc64"
,"powerpc64el"
,"sparc"
,"amd64"
,"mips"
,"mipsel"
,"arm"
,"arm64"
,"mips64"
,"mips64el"
,"riscv32"
,"riscv64"
, '"loongarch64"'. -
hostOS {.magic: "HostOS".}: string = ""
-
A string that describes the host operating system.
Possible values:
Source Edit"windows"
,"macosx"
,"linux"
,"netbsd"
,"freebsd"
,"openbsd"
,"solaris"
,"aix"
,"haiku"
,"standalone"
. -
Inf = 0x7FF0000000000000'f64
- Contains the IEEE floating point value of positive infinity. Source Edit
-
isMainModule {.magic: "IsMainModule".}: bool = false
- True only when accessed in the main module. This works thanks to compiler magic. It is useful to embed testing code in a module. Source Edit
-
NaN = 0x7FF7FFFFFFFFFFFF'f64
-
Contains an IEEE floating point value of Not A Number.
Note that you cannot compare a floating point value to this value and expect a reasonable result - use the
Source EditisNaN
orclassify
procedure in the math module for checking for NaN. -
NegInf = 0xFFF0000000000000'f64
- Contains the IEEE floating point value of negative infinity. Source Edit
-
NimMajor {.intdefine.}: int = 2
-
is the major number of Nim's version. Example:
Source Editwhen (NimMajor, NimMinor, NimPatch) >= (1, 3, 1): discard
-
NimMinor {.intdefine.}: int = 0
- is the minor number of Nim's version. Odd for devel, even for releases. Source Edit
Procs
-
proc `&`(x, y: char): string {.magic: "ConStrStr", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Concatenates characters
x
andy
into a string.
Source Editassert('a' & 'b' == "ab")
-
proc `&`(x, y: string): string {.magic: "ConStrStr", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Concatenates strings
x
andy
.
Source Editassert("ab" & "cd" == "abcd")
-
proc `&`(x: char; y: string): string {.magic: "ConStrStr", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Concatenates
x
withy
.
Source Editassert('a' & "bc" == "abc")
-
proc `&`(x: string; y: char): string {.magic: "ConStrStr", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Concatenates
x
withy
.
Source Editassert("ab" & 'c' == "abc")
-
proc `&`[T](x, y: seq[T]): seq[T] {.noSideEffect.}
-
Concatenates two sequences.
Requires copying of the sequences.
assert(@[1, 2, 3, 4] & @[5, 6] == @[1, 2, 3, 4, 5, 6])
See also:
Source Edit -
proc `*`(x, y: float): float {.magic: "MulF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `*`(x, y: float32): float32 {.magic: "MulF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `*`(x, y: int): int {.magic: "MulI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Binary
*
operator for an integer. Source Edit -
proc `*`(x, y: int8): int8 {.magic: "MulI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `*`(x, y: int16): int16 {.magic: "MulI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `*`(x, y: int32): int32 {.magic: "MulI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `*`(x, y: int64): int64 {.magic: "MulI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `*`(x, y: uint): uint {.magic: "MulU", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Binary
*
operator for unsigned integers. Source Edit -
proc `*`(x, y: uint8): uint8 {.magic: "MulU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `*`(x, y: uint16): uint16 {.magic: "MulU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `*`(x, y: uint32): uint32 {.magic: "MulU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `+`(x, y: float): float {.magic: "AddF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `+`(x, y: float32): float32 {.magic: "AddF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `+`(x, y: int): int {.magic: "AddI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Binary
+
operator for an integer. Source Edit -
proc `+`(x, y: int8): int8 {.magic: "AddI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `+`(x, y: int16): int16 {.magic: "AddI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `+`(x, y: int32): int32 {.magic: "AddI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `+`(x, y: int64): int64 {.magic: "AddI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `+`(x, y: uint): uint {.magic: "AddU", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Binary
+
operator for unsigned integers. Source Edit -
proc `+`(x, y: uint8): uint8 {.magic: "AddU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `+`(x, y: uint16): uint16 {.magic: "AddU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `+`(x, y: uint32): uint32 {.magic: "AddU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `+`(x, y: uint64): uint64 {.magic: "AddU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `+`(x: float): float {.magic: "UnaryPlusF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `+`(x: float32): float32 {.magic: "UnaryPlusF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `+`(x: int): int {.magic: "UnaryPlusI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Unary
+
operator for an integer. Has no effect. Source Edit -
proc `+`(x: int8): int8 {.magic: "UnaryPlusI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `+`(x: int16): int16 {.magic: "UnaryPlusI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `+`(x: int32): int32 {.magic: "UnaryPlusI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `-`(x, y: float): float {.magic: "SubF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `-`(x, y: float32): float32 {.magic: "SubF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `-`(x, y: int): int {.magic: "SubI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Binary
-
operator for an integer. Source Edit -
proc `-`(x, y: int8): int8 {.magic: "SubI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `-`(x, y: int16): int16 {.magic: "SubI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `-`(x, y: int32): int32 {.magic: "SubI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `-`(x, y: int64): int64 {.magic: "SubI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `-`(x, y: uint): uint {.magic: "SubU", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Binary
-
operator for unsigned integers. Source Edit -
proc `-`(x, y: uint8): uint8 {.magic: "SubU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `-`(x, y: uint16): uint16 {.magic: "SubU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `-`(x, y: uint32): uint32 {.magic: "SubU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `-`(x, y: uint64): uint64 {.magic: "SubU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `-`(x: float): float {.magic: "UnaryMinusF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `-`(x: float32): float32 {.magic: "UnaryMinusF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `-`(x: int): int {.magic: "UnaryMinusI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Unary
-
operator for an integer. Negatesx
. Source Edit -
proc `-`(x: int8): int8 {.magic: "UnaryMinusI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `-`(x: int16): int16 {.magic: "UnaryMinusI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `-`(x: int32): int32 {.magic: "UnaryMinusI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `..`[T, U](a: sink T; b: sink U): HSlice[T, U] {.noSideEffect, inline, magic: "DotDot", ...raises: [], tags: [], forbids: [].}
-
Binary slice operator that constructs an interval
[a, b]
, botha
andb
are inclusive.Slices can also be used in the set constructor and in ordinal case statements, but then they are special-cased by the compiler.
Source Editlet a = [10, 20, 30, 40, 50] echo a[2 .. 3] # @[30, 40]
-
proc `/`(x, y: float): float {.magic: "DivF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<`(x, y: bool): bool {.magic: "LtB", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<`(x, y: char): bool {.magic: "LtCh", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Compares two chars and returns true if
x
is lexicographically beforey
(uppercase letters come before lowercase letters).Example:
Source Editlet a = 'a' b = 'b' c = 'Z' assert a < b assert not (a < a) assert not (a < c)
-
proc `<`(x, y: float): bool {.magic: "LtF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<`(x, y: float32): bool {.magic: "LtF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<`(x, y: int): bool {.magic: "LtI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns true if
x
is less thany
. Source Edit -
proc `<`(x, y: int8): bool {.magic: "LtI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<`(x, y: int16): bool {.magic: "LtI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<`(x, y: int32): bool {.magic: "LtI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<`(x, y: int64): bool {.magic: "LtI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<`(x, y: pointer): bool {.magic: "LtPtr", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<`(x, y: string): bool {.magic: "LtStr", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Compares two strings and returns true if
x
is lexicographically beforey
(uppercase letters come before lowercase letters).Example:
Source Editlet a = "abc" b = "abd" c = "ZZZ" assert a < b assert not (a < a) assert not (a < c)
-
proc `<`(x, y: uint): bool {.magic: "LtU", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns true if
x < y
. Source Edit -
proc `<`(x, y: uint8): bool {.magic: "LtU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<`(x, y: uint16): bool {.magic: "LtU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<`(x, y: uint32): bool {.magic: "LtU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<`(x, y: uint64): bool {.magic: "LtU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<`[Enum: enum](x, y: Enum): bool {.magic: "LtEnum", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<`[T: tuple](x, y: T): bool
-
Generic lexicographic
<
operator for tuples that is lifted from the components ofx
andy
. This implementation usescmp
. Source Edit -
proc `<`[T](x, y: ptr T): bool {.magic: "LtPtr", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<`[T](x, y: ref T): bool {.magic: "LtPtr", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<`[T](x, y: set[T]): bool {.magic: "LtSet", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns true if
x
is a strict or proper subset ofy
.A strict or proper subset
x
has all of its members iny
buty
has more elements thany
.Example:
Source Editlet a = {3, 5} b = {1, 3, 5, 7} c = {2} assert a < b assert not (a < a) assert not (a < c)
-
proc `<=`(x, y: bool): bool {.magic: "LeB", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<=`(x, y: char): bool {.magic: "LeCh", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Compares two chars and returns true if
x
is lexicographically beforey
(uppercase letters come before lowercase letters).Example:
Source Editlet a = 'a' b = 'b' c = 'Z' assert a <= b assert a <= a assert not (a <= c)
-
proc `<=`(x, y: float): bool {.magic: "LeF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<=`(x, y: float32): bool {.magic: "LeF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<=`(x, y: int): bool {.magic: "LeI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns true if
x
is less than or equal toy
. Source Edit -
proc `<=`(x, y: int8): bool {.magic: "LeI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<=`(x, y: int16): bool {.magic: "LeI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<=`(x, y: int32): bool {.magic: "LeI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<=`(x, y: int64): bool {.magic: "LeI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<=`(x, y: pointer): bool {.magic: "LePtr", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<=`(x, y: string): bool {.magic: "LeStr", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Compares two strings and returns true if
x
is lexicographically beforey
(uppercase letters come before lowercase letters).Example:
Source Editlet a = "abc" b = "abd" c = "ZZZ" assert a <= b assert a <= a assert not (a <= c)
-
proc `<=`(x, y: uint): bool {.magic: "LeU", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns true if
x <= y
. Source Edit -
proc `<=`(x, y: uint8): bool {.magic: "LeU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<=`(x, y: uint16): bool {.magic: "LeU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<=`(x, y: uint32): bool {.magic: "LeU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<=`(x, y: uint64): bool {.magic: "LeU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<=`[Enum: enum](x, y: Enum): bool {.magic: "LeEnum", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<=`[T: tuple](x, y: T): bool
-
Generic lexicographic
<=
operator for tuples that is lifted from the components ofx
andy
. This implementation usescmp
. Source Edit -
proc `<=`[T](x, y: ref T): bool {.magic: "LePtr", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `<=`[T](x, y: set[T]): bool {.magic: "LeSet", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns true if
x
is a subset ofy
.A subset
x
has all of its members iny
andy
doesn't necessarily have more members thanx
. That is,x
can be equal toy
.Example:
Source Editlet a = {3, 5} b = {1, 3, 5, 7} c = {2} assert a <= b assert a <= a assert not (a <= c)
-
proc `==`(x, y: bool): bool {.magic: "EqB", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Checks for equality between two
bool
variables. Source Edit -
proc `==`(x, y: char): bool {.magic: "EqCh", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Checks for equality between two
char
variables. Source Edit -
proc `==`(x, y: cstring): bool {.magic: "EqCString", noSideEffect, inline, ...raises: [], tags: [], forbids: [].}
-
Checks for equality between two
cstring
variables. Source Edit -
proc `==`(x, y: float): bool {.magic: "EqF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `==`(x, y: float32): bool {.magic: "EqF64", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `==`(x, y: int): bool {.magic: "EqI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Compares two integers for equality. Source Edit
-
proc `==`(x, y: int8): bool {.magic: "EqI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `==`(x, y: int16): bool {.magic: "EqI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `==`(x, y: int32): bool {.magic: "EqI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `==`(x, y: int64): bool {.magic: "EqI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `==`(x, y: pointer): bool {.magic: "EqRef", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Checks for equality between two
pointer
variables.Example:
Source Editvar # this is a wildly dangerous example a = cast[pointer](0) b = cast[pointer](nil) assert a == b # true due to the special meaning of `nil`/0 as a pointer
-
proc `==`(x, y: string): bool {.magic: "EqStr", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Checks for equality between two
string
variables. Source Edit -
proc `==`(x, y: uint): bool {.magic: "EqI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Compares two unsigned integers for equality. Source Edit
-
proc `==`(x, y: uint8): bool {.magic: "EqI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `==`(x, y: uint16): bool {.magic: "EqI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `==`(x, y: uint32): bool {.magic: "EqI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `==`(x, y: uint64): bool {.magic: "EqI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `==`[Enum: enum](x, y: Enum): bool {.magic: "EqEnum", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Checks whether values within the same enum have the same underlying value.
Example:
Source Edittype Enum1 = enum field1 = 3, field2 Enum2 = enum place1, place2 = 3 var e1 = field1 e2 = place2.ord.Enum1 assert e1 == e2 assert not compiles(e1 == place2) # raises error
-
proc `==`[T: proc | iterator](x, y: T): bool {.magic: "EqProc", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Checks that two
proc
variables refer to the same procedure. Source Edit -
proc `==`[T: tuple | object](x, y: T): bool
-
Generic
==
operator for tuples that is lifted from the components. ofx
andy
. Source Edit -
proc `==`[T](x, y: ptr T): bool {.magic: "EqRef", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Checks that two
ptr
variables refer to the same item. Source Edit -
proc `==`[T](x, y: ref T): bool {.magic: "EqRef", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Checks that two
ref
variables refer to the same item. Source Edit -
proc `@`[IDX, T](a: sink array[IDX, T]): seq[T] {.magic: "ArrToSeq", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Turns an array into a sequence.
This most often useful for constructing sequences with the array constructor:
@[1, 2, 3]
has the typeseq[int]
, while[1, 2, 3]
has the typearray[0..2, int]
.
Source Editlet a = [1, 3, 5] b = "foo" echo @a # => @[1, 3, 5] echo @b # => @['f', 'o', 'o']
-
proc `[]`(s: string; i: BackwardsIndex): char {.inline, systemRaisesDefect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `[]`(s: var string; i: BackwardsIndex): var char {.inline, systemRaisesDefect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `[]`[I: Ordinal; T](a: T; i: I): T {.noSideEffect, magic: "ArrGet", ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `[]`[Idx, T; U, V: Ordinal](a: array[Idx, T]; x: HSlice[U, V]): seq[T] {. systemRaisesDefect.}
-
Slice operation for arrays. Returns the inclusive range
[a[x.a], a[x.b]]
:
Source Editvar a = [1, 2, 3, 4] assert a[0..2] == @[1, 2, 3]
-
proc `[]`[Idx, T](a: array[Idx, T]; i: BackwardsIndex): T {.inline, systemRaisesDefect.}
- Source Edit
-
proc `[]`[Idx, T](a: var array[Idx, T]; i: BackwardsIndex): var T {.inline, systemRaisesDefect.}
- Source Edit
-
proc `[]`[T, U: Ordinal](s: string; x: HSlice[T, U]): string {.inline, systemRaisesDefect.}
-
Slice operation for strings. Returns the inclusive range
[s[x.a], s[x.b]]
:
Source Editvar s = "abcdef" assert s[1..3] == "bcd"
-
proc `[]=`(s: var string; i: BackwardsIndex; x: char) {.inline, systemRaisesDefect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `[]=`[I: Ordinal; T, S](a: T; i: I; x: sink S) {.noSideEffect, magic: "ArrPut", ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `[]=`[Idx, T; U, V: Ordinal](a: var array[Idx, T]; x: HSlice[U, V]; b: openArray[T]) {.systemRaisesDefect.}
-
Slice assignment for arrays.
Source Editvar a = [10, 20, 30, 40, 50] a[1..2] = @[99, 88] assert a == [10, 99, 88, 40, 50]
-
proc `[]=`[Idx, T](a: var array[Idx, T]; i: BackwardsIndex; x: T) {.inline, systemRaisesDefect.}
- Source Edit
-
proc `[]=`[T, U: Ordinal](s: var string; x: HSlice[T, U]; b: string) {. systemRaisesDefect.}
-
Slice assignment for strings.
If
b.len
is not exactly the number of elements that are referred to byx
, a splice is performed:Example:
Source Editvar s = "abcdefgh" s[1 .. ^2] = "xyz" assert s == "axyzh"
-
proc `[]=`[T; U, V: Ordinal](s: var seq[T]; x: HSlice[U, V]; b: openArray[T]) {. systemRaisesDefect.}
-
Slice assignment for sequences.
If
b.len
is not exactly the number of elements that are referred to byx
, a splice is performed.Example:
Source Editvar s = @"abcdefgh" s[1 .. ^2] = @"xyz" assert s == @"axyzh"
-
func abs(x: int16): int16 {.magic: "AbsI", inline, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
func abs(x: int32): int32 {.magic: "AbsI", inline, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc add(x: var cstring; y: cstring) {.magic: "AppendStrStr", ...raises: [], tags: [], forbids: [].}
-
Appends
y
tox
in place. Only implemented for JS backend.Example:
Source Editwhen defined(js): var tmp: cstring = "" tmp.add(cstring("ab")) tmp.add(cstring("cd")) doAssert tmp == cstring("abcd")
-
proc add(x: var string; y: char) {.magic: "AppendStrCh", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Appends
y
tox
in place.
Source Editvar tmp = "" tmp.add('a') tmp.add('b') assert(tmp == "ab")
-
proc add(x: var string; y: cstring) {.asmNoStackFrame, ...raises: [], tags: [], forbids: [].}
-
Appends
y
tox
in place.Example:
Source Editvar tmp = "" tmp.add(cstring("ab")) tmp.add(cstring("cd")) doAssert tmp == "abcd"
-
proc add(x: var string; y: string) {.magic: "AppendStrStr", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Concatenates
x
andy
in place.See also
strbasics.add
.Example:
Source Editvar tmp = "" tmp.add("ab") tmp.add("cd") assert tmp == "abcd"
-
proc add[T](x: var seq[T]; y: openArray[T]) {.noSideEffect.}
-
Generic proc for adding a container
y
to a containerx
.For containers that have an order,
add
means append. New generic containers should also call their adding procadd
for consistency. Generic code becomes much easier to write if the Nim naming scheme is respected.See also:
Example:
Source Editvar a = @["a1", "a2"] a.add(["b1", "b2"]) assert a == @["a1", "a2", "b1", "b2"] var c = @["c0", "c1", "c2", "c3"] a.add(c.toOpenArray(1, 2)) assert a == @["a1", "a2", "b1", "b2", "c1", "c2"]
-
proc add[T](x: var seq[T]; y: sink T) {.magic: "AppendSeqElem", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Generic proc for adding a data item
y
to a containerx
.For containers that have an order,
add
means append. New generic containers should also call their adding procadd
for consistency. Generic code becomes much easier to write if the Nim naming scheme is respected.
Source Editvar s: seq[string] = @["test2","test2"] s.add("test") assert s == @["test2", "test2", "test"]
-
proc addEscapedChar(s: var string; c: char) {.noSideEffect, inline, ...raises: [], tags: [], forbids: [].}
-
Adds a char to string
s
and applies the following escaping:- replaces any
\
by\\
- replaces any
'
by\'
- replaces any
"
by\"
- replaces any
\a
by\\a
- replaces any
\b
by\\b
- replaces any
\t
by\\t
- replaces any
\n
by\\n
- replaces any
\v
by\\v
- replaces any
\f
by\\f
- replaces any
\r
by\\r
- replaces any
\e
by\\e
- replaces any other character not in the set
{\21..\126}
by\xHH
whereHH
is its hexadecimal value
The procedure has been designed so that its output is usable for many different common syntaxes.
Warning: This is not correct for producing ANSI C code!Source Edit - replaces any
-
proc addQuitProc(quitProc: proc () {.noconv.}) {.importc: "atexit", header: "<stdlib.h>", ...deprecated: "use exitprocs.addExitProc", raises: [], tags: [], forbids: [].}
-
Adds/registers a quit procedure.
Each call to
Source EditaddQuitProc
registers another quit procedure. Up to 30 procedures can be registered. They are executed on a last-in, first-out basis (that is, the last function registered is the first to be executed).addQuitProc
raises an EOutOfIndex exception ifquitProc
cannot be registered. -
proc addQuoted[T](s: var string; x: T)
-
Appends
x
to strings
in place, applying quoting and escaping ifx
is a string or char.See addEscapedChar for the escaping scheme. When
x
is a string, characters in the range{\128..\255}
are never escaped so that multibyte UTF-8 characters are untouched (note that this behavior is different fromaddEscapedChar
).The Nim standard library uses this function on the elements of collections when producing a string representation of a collection. It is recommended to use this function as well for user-side collections. Users may overload
addQuoted
for custom (string-like) types if they want to implement a customized element representation.
Source Editvar tmp = "" tmp.addQuoted(1) tmp.add(", ") tmp.addQuoted("string") tmp.add(", ") tmp.addQuoted('c') assert(tmp == """1, "string", 'c'""")
-
proc `addr`[T](x: T): ptr T {.magic: "Addr", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Builtin
addr
operator for taking the address of a memory location.Note: This works forlet
variables or parameters for better interop with C. When you use it to write a wrapper for a C library and take the address oflet
variables or parameters, you should always check that the original library does never write to data behind the pointer that is returned from this procedure.Cannot be overloaded.
Source Editvar buf: seq[char] = @['a','b','c'] p = buf[1].addr echo p.repr # ref 0x7faa35c40059 --> 'b' echo p[] # b
-
proc `and`(a, b: typedesc): typedesc {.magic: "TypeTrait", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Constructs an
and
meta class. Source Edit -
proc `and`(x, y: bool): bool {.magic: "And", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Boolean
and
; returns true ifx == y == true
(if both arguments are true).Evaluation is lazy: if
Source Editx
is false,y
will not even be evaluated. -
proc `and`(x, y: int): int {.magic: "BitandI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Computes the
bitwise and
of numbersx
andy
.Example:
Source Editassert (0b0011 and 0b0101) == 0b0001 assert (0b0111 and 0b1100) == 0b0100
-
proc `and`(x, y: int8): int8 {.magic: "BitandI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `and`(x, y: int16): int16 {.magic: "BitandI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `and`(x, y: int32): int32 {.magic: "BitandI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `and`(x, y: int64): int64 {.magic: "BitandI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `and`(x, y: uint): uint {.magic: "BitandI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Computes the
bitwise and
of numbersx
andy
. Source Edit -
proc `and`(x, y: uint8): uint8 {.magic: "BitandI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `and`(x, y: uint16): uint16 {.magic: "BitandI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc ashr(x: int8; y: SomeInteger): int8 {.magic: "AshrI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc ashr(x: int16; y: SomeInteger): int16 {.magic: "AshrI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc ashr(x: int32; y: SomeInteger): int32 {.magic: "AshrI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc ashr(x: int64; y: SomeInteger): int64 {.magic: "AshrI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc ashr(x: int; y: SomeInteger): int {.magic: "AshrI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off.
Note that
ashr
is not an operator so use the normal function call syntax for it.See also:
Example:
Source Editassert ashr(0b0001_0000'i8, 2) == 0b0000_0100'i8 assert ashr(0b1000_0000'i8, 8) == 0b1111_1111'i8 assert ashr(0b1000_0000'i8, 1) == 0b1100_0000'i8
-
func chr(u: range[0 .. 255]): char {.magic: "Chr", ...raises: [], tags: [], forbids: [].}
-
Converts
u
to achar
, same aschar(u)
.Example:
Source EditdoAssert chr(65) == 'A' doAssert chr(255) == '\255' doAssert chr(255) == char(255) doAssert not compiles chr(256) doAssert not compiles char(256) var x = 256 doAssertRaises(RangeDefect): discard chr(x) doAssertRaises(RangeDefect): discard char(x)
-
proc clamp[T](x, a, b: T): T
-
Limits the value
x
within the interval [a, b]. This proc is equivalent to but faster thanmax(a, min(b, x))
.Warning:a <= b
is assumed and will not be checked (currently).See also:
math.clamp
for a version that takes aSlice[T]
instead.Example:
Source Editassert (1.4).clamp(0.0, 1.0) == 1.0 assert (0.5).clamp(0.0, 1.0) == 0.5 assert 4.clamp(1, 3) == max(1, min(3, 4))
-
proc cmp(x, y: string): int {.noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Compare proc for strings. More efficient than the generic version.
Note: The precise result values depend on the used C runtime library and can differ between operating systems!
Source Edit -
proc cmp[T](x, y: T): int
-
Generic compare proc.
Returns:
- a value less than zero, if
x < y
- a value greater than zero, if
x > y
- zero, if
x == y
This is useful for writing generic algorithms without performance loss. This generic implementation uses the
==
and<
operators.
Source Editimport std/algorithm echo sorted(@[4, 2, 6, 5, 8, 7], cmp[int])
- a value less than zero, if
-
proc cmpMem(a, b: pointer; size: Natural): int {.inline, noSideEffect, ...tags: [], raises: [], forbids: [].}
-
Compares the memory blocks
a
andb
.size
bytes will be compared.Returns:
- a value less than zero, if
a < b
- a value greater than zero, if
a > b
- zero, if
a == b
Like any procedure dealing with raw memory this is unsafe.
Source Edit - a value less than zero, if
-
proc compileOption(option, arg: string): bool {.magic: "CompileOptionArg", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Can be used to determine an enum compile-time option.
See also:
- compileOption for
on|off
options - defined
- std/compilesettings module
Example:
Source Editwhen compileOption("opt", "size") and compileOption("gc", "boehm"): discard "compiled with optimization for size and uses Boehm's GC"
- compileOption for
-
proc compileOption(option: string): bool {.magic: "CompileOption", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Can be used to determine an
on|off
compile-time option.See also:
- compileOption for enum options
- defined
- std/compilesettings module
Example: cmd: --floatChecks:off
Source Editstatic: doAssert not compileOption("floatchecks") {.push floatChecks: on.} static: doAssert compileOption("floatchecks") # floating point NaN and Inf checks enabled in this scope {.pop.}
-
proc compiles(x: untyped): bool {.magic: "Compiles", noSideEffect, compileTime, ...raises: [], tags: [], forbids: [].}
-
Special compile-time procedure that checks whether
x
can be compiled without any semantic error. This can be used to check whether a type supports some operation:
Source Editwhen compiles(3 + 4): echo "'+' for integers is available"
-
proc contains[T](a: openArray[T]; item: T): bool {.inline.}
-
Returns true if
item
is ina
or false if not found. This is a shortcut forfind(a, item) >= 0
.This allows the
in
operator:a.contains(item)
is the same asitem in a
.
Source Editvar a = @[1, 3, 5] assert a.contains(5) assert 3 in a assert 99 notin a
-
func contains[T](x: set[T]; y: T): bool {.magic: "InSet", ...raises: [], tags: [], forbids: [].}
-
One should overload this proc if one wants to overload the
in
operator.The parameters are in reverse order!
a in b
is a template forcontains(b, a)
. This is because the unification algorithm that Nim uses for overload resolution works from left to right. But for thein
operator that would be the wrong direction for this piece of code:Example:
Ifvar s: set[range['a'..'z']] = {'a'..'c'} assert s.contains('c') assert 'b' in s assert 'd' notin s assert set['a'..'z'] is set[range['a'..'z']]
in
had been declared as[T](elem: T, s: set[T])
thenT
would have been bound tochar
. Buts
is not compatible to typeset[char]
! The solution is to bindT
torange['a'..'z']
. This is achieved by reversing the parameters forcontains
;in
then passes its arguments in reverse order. Source Edit -
proc copyMem(dest, source: pointer; size: Natural) {.inline, ...gcsafe, tags: [], raises: [], forbids: [].}
-
Copies the contents from the memory at
source
to the memory atdest
. Exactlysize
bytes will be copied. The memory regions may not overlap. Like any procedure dealing with raw memory this is unsafe. Source Edit -
proc create(T: typedesc; size = 1.Positive): ptr T:type {.inline, ...gcsafe, raises: [].}
-
Allocates a new memory block with at least
T.sizeof * size
bytes.The block has to be freed with resize(block, 0) or dealloc(block). The block is initialized with all bytes containing zero, so it is somewhat safer than createU.
The allocated memory belongs to its allocating thread! Use createShared to allocate from a shared heap.
Source Edit -
proc createU(T: typedesc; size = 1.Positive): ptr T:type {.inline, ...gcsafe, raises: [].}
-
Allocates a new memory block with at least
T.sizeof * size
bytes.The block has to be freed with resize(block, 0) or dealloc(block). The block is not initialized, so reading from it before writing to it is undefined behaviour!
The allocated memory belongs to its allocating thread! Use createSharedU to allocate from a shared heap.
See also:
Source Edit -
proc dealloc(p: pointer) {.noconv, compilerproc, ...gcsafe, gcsafe, raises: [], tags: [], forbids: [].}
-
Frees the memory allocated with
alloc
,alloc0
,realloc
,create
orcreateU
.This procedure is dangerous! If one forgets to free the memory a leak occurs; if one tries to access freed memory (or just freeing it twice!) a core dump may happen or other memory may be corrupted.
The freed memory must belong to its allocating thread! Use deallocShared to deallocate from a shared heap.
Source Edit -
proc deallocHeap(runFinalizers = true; allowGcAfterwards = true) {....raises: [], tags: [RootEffect], forbids: [].}
-
Frees the thread local heap. Runs every finalizer if
runFinalizers
is true. IfallowGcAfterwards
is true, a minimal amount of allocation happens to ensure the GC can continue to work after the call todeallocHeap
. Source Edit -
proc debugEcho(x: varargs[typed, `$`]) {.magic: "Echo", noSideEffect, ...tags: [], raises: [], forbids: [].}
-
Same as echo, but as a special semantic rule,
debugEcho
pretends to be free of side effects, so that it can be used for debugging routines marked as noSideEffect. Source Edit -
proc dec[T, V: Ordinal](x: var T; y: V = 1) {.magic: "Dec", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Decrements the ordinal
x
byy
.If such a value does not exist,
OverflowDefect
is raised or a compile time error occurs. This is a short notation for:x = pred(x, y)
.Example:
Source Editvar i = 2 dec(i) assert i == 1 dec(i, 3) assert i == -2
-
proc declared(x: untyped): bool {.magic: "Declared", noSideEffect, compileTime, ...raises: [], tags: [], forbids: [].}
-
Special compile-time procedure that checks whether
x
is declared.x
has to be an identifier or a qualified identifier.This can be used to check whether a library provides a certain feature or not:
when not declared(strutils.toUpper): # provide our own toUpper proc here, because strutils is # missing it.
See also:
Source Edit -
proc deepCopy[T](x: var T; y: T) {.noSideEffect, magic: "DeepCopy", ...raises: [], tags: [], forbids: [].}
-
Performs a deep copy of
y
and copies it intox
.This is also used by the code generator for the implementation of
spawn
.For
Source Edit--gc:arc
or--gc:orc
deepcopy support has to be enabled via--deepcopy:on
. -
proc default[T](__33556008: typedesc[T]): T {.magic: "Default", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns the default value of the type
T
. Contrary tozeroDefault
, it takes default fields of an object into consideration.See also:
Example: cmd: -d:nimPreviewRangeDefault
Source Editassert (int, float).default == (0, 0.0) type Foo = object a: range[2..6] var x = Foo.default assert x.a == 2
-
proc defined(x: untyped): bool {.magic: "Defined", noSideEffect, compileTime, ...raises: [], tags: [], forbids: [].}
-
Special compile-time procedure that checks whether
x
is defined.x
is an external symbol introduced through the compiler's -d:x switch to enable build time conditionals:when not defined(release): # Do here programmer friendly expensive sanity checks. # Put here the normal code
See also:
- compileOption for
on|off
options - compileOption for enum options
- define pragmas
- compileOption for
-
proc delete[T](x: var seq[T]; i: Natural) {.noSideEffect.}
-
Deletes the item at index
i
by moving allx[i+1..^1]
items by one position.This is an
O(n)
operation.Note: With-d:nimStrictDelete
, an index error is produced when the index passed to it was out of bounds.-d:nimStrictDelete
will become the default in upcoming versions.See also:
- del for O(1) operation
Example:
Source Editvar s = @[1, 2, 3, 4, 5] s.delete(2) doAssert s == @[1, 2, 4, 5]
-
proc `div`(x, y: int): int {.magic: "DivI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Computes the integer division.
This is roughly the same as
math.trunc(x/y).int
.Example:
Source Editassert (1 div 2) == 0 assert (2 div 2) == 1 assert (3 div 2) == 1 assert (7 div 3) == 2 assert (-7 div 3) == -2 assert (7 div -3) == -2 assert (-7 div -3) == 2
-
proc `div`(x, y: int8): int8 {.magic: "DivI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `div`(x, y: int16): int16 {.magic: "DivI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `div`(x, y: int32): int32 {.magic: "DivI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `div`(x, y: int64): int64 {.magic: "DivI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `div`(x, y: uint): uint {.magic: "DivU", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Computes the integer division for unsigned integers. This is roughly the same as
trunc(x/y)
. Source Edit -
proc `div`(x, y: uint8): uint8 {.magic: "DivU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `div`(x, y: uint16): uint16 {.magic: "DivU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc echo(x: varargs[typed, `$`]) {.magic: "Echo", ...gcsafe, sideEffect, ...raises: [], tags: [], forbids: [].}
-
Writes and flushes the parameters to the standard output.
Special built-in that takes a variable number of arguments. Each argument is converted to a string via
$
, so it works for user-defined types that have an overloaded$
operator. It is roughly equivalent towriteLine(stdout, x); flushFile(stdout)
, but available for the JavaScript target too.Unlike other IO operations this is guaranteed to be thread-safe as
Source Editecho
is very often used for debugging convenience. If you want to useecho
inside a proc without side effects you can use debugEcho instead. -
proc equalMem(a, b: pointer; size: Natural): bool {.inline, noSideEffect, ...tags: [], raises: [], forbids: [].}
-
Compares the memory blocks
a
andb
.size
bytes will be compared.If the blocks are equal,
Source Edittrue
is returned,false
otherwise. Like any procedure dealing with raw memory this is unsafe. -
proc GC_disable() {....gcsafe, inline, ...gcsafe, raises: [], tags: [], forbids: [].}
-
Disables the GC. If called
n
times,n
calls toGC_enable
are needed to reactivate the GC.Note that in most circumstances one should only disable the mark and sweep phase with GC_disableMarkAndSweep.
Source Edit -
proc GC_disableMarkAndSweep() {....gcsafe, gcsafe, raises: [], tags: [], forbids: [].}
- The current implementation uses a reference counting garbage collector with a seldomly run mark and sweep phase to free cycles. The mark and sweep phase may take a long time and is not needed if the application does not create cycles. Thus the mark and sweep phase can be deactivated and activated separately from the rest of the GC. Source Edit
-
proc GC_ref(x: string) {.magic: "GCref", ...gcsafe, raises: [], tags: [], forbids: [].}
-
Marks the object
x
as referenced, so that it will not be freed until it is unmarked viaGC_unref
. If called n-times for the same objectx
, n calls toGC_unref
are needed to unmarkx
. Source Edit -
proc GC_unref(x: string) {.magic: "GCunref", ...gcsafe, raises: [], tags: [], forbids: [].}
- See the documentation of GC_ref. Source Edit
-
proc getTypeInfo[T](x: T): pointer {.magic: "GetTypeInfo", ...gcsafe, raises: [], tags: [], forbids: [].}
-
Get type information for
x
.Ordinary code should not use this, but the typeinfo module instead.
Source Edit -
proc gorge(command: string; input = ""; cache = ""): string {. magic: "StaticExec", ...raises: [], tags: [], forbids: [].}
- This is an alias for staticExec. Source Edit
-
proc high(x: cstring): int {.magic: "High", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns the highest possible index of a compatible string
x
. This is sometimes an O(n) operation.See also:
Source Edit -
proc high(x: string): int {.magic: "High", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns the highest possible index of a string
x
.var str = "Hello world!" high(str) # => 11
See also:
Source Edit -
proc high[I, T](x: array[I, T]): I {.magic: "High", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns the highest possible index of an array
x
.For empty arrays, the return type is
int
.var arr = [1, 2, 3, 4, 5, 6, 7] high(arr) # => 6 for i in low(arr)..high(arr): echo arr[i]
See also:
Source Edit -
proc high[I, T](x: typedesc[array[I, T]]): I {.magic: "High", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns the highest possible index of an array type.
For empty arrays, the return type is
int
.high(array[7, int]) # => 6
See also:
Source Edit -
proc high[T: Ordinal | enum | range](x: T): T {.magic: "High", noSideEffect, ...deprecated: "Deprecated since v1.4; there should not be `high(value)`. Use `high(type)`.", raises: [], tags: [], forbids: [].}
-
Returns the highest possible value of an ordinal value
x
.As a special semantic rule,
x
may also be a type identifier.This proc is deprecated, use this one instead:
Source Edithigh(2) # => 9223372036854775807
-
proc high[T: Ordinal | enum | range](x: typedesc[T]): T {.magic: "High", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns the highest possible value of an ordinal or enum type.
high(int)
is Nim's way of writing INT_MAX or MAX_INT.high(int) # => 9223372036854775807
See also:
Source Edit -
proc inc[T, V: Ordinal](x: var T; y: V = 1) {.magic: "Inc", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Increments the ordinal
x
byy
.If such a value does not exist,
OverflowDefect
is raised or a compile time error occurs. This is a short notation for:x = succ(x, y)
.Example:
Source Editvar i = 2 inc(i) assert i == 3 inc(i, 3) assert i == 6
-
proc instantiationInfo(index = -1; fullPaths = false): tuple[filename: string, line: int, column: int] {.magic: "InstantiationInfo", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Provides access to the compiler's instantiation stack line information of a template.
While similar to the caller info of other languages, it is determined at compile time.
This proc is mostly useful for meta programming (eg.
assert
template) to retrieve information about the current filename and line number. Example:
Source Editimport std/strutils template testException(exception, code: untyped): typed = try: let pos = instantiationInfo() discard(code) echo "Test failure at $1:$2 with '$3'" % [pos.filename, $pos.line, astToStr(code)] assert false, "A test expecting failure succeeded?" except exception: discard proc tester(pos: int): int = let a = @[1, 2, 3] result = a[pos] when isMainModule: testException(IndexDefect, tester(30)) testException(IndexDefect, tester(1)) # --> Test failure at example.nim:20 with 'tester(1)'
-
proc `is`[T, S](x: T; y: S): bool {.magic: "Is", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Checks if
T
is of the same type asS
.For a negated version, use isnot.
Source Editassert 42 is int assert @[1, 2] is seq proc test[T](a: T): int = when (T is int): return a else: return 0 assert(test[int](3) == 3) assert(test[string]("xyz") == 0)
-
proc isNil(x: cstring): bool {.noSideEffect, magic: "IsNil", ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc isNil(x: pointer): bool {.noSideEffect, magic: "IsNil", ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc isNil[T: proc | iterator {.closure.}](x: T): bool {.noSideEffect, magic: "IsNil", ...raises: [], tags: [], forbids: [].}
-
Fast check whether
x
is nil. This is sometimes more efficient than== nil
. Source Edit -
func len(x: (type array) | array): int {.magic: "LengthArray", ...raises: [], tags: [], forbids: [].}
-
Returns the length of an array or an array type. This is roughly the same as
high(T)-low(T)+1
.Example:
Source Editvar a = [1, 1, 1] assert a.len == 3 assert array[0, float].len == 0 static: assert array[-2..2, float].len == 5
-
proc len(x: cstring): int {.magic: "LengthStr", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns the length of a compatible string. This is an O(n) operation except in js at runtime.
Note: On the JS backend this currently counts UTF-16 code points instead of bytes at runtime (not at compile time). For now, if you need the byte length of the UTF-8 encoding, convert to string with
$
first then calllen
.Example:
Source EditdoAssert len(cstring"abc") == 3 doAssert len(cstring r"ab\0c") == 5 # \0 is escaped doAssert len(cstring"ab\0c") == 5 # ditto var a: cstring = "ab\0c" when defined(js): doAssert a.len == 4 # len ignores \0 for js else: doAssert a.len == 2 # \0 is a null terminator static: var a2: cstring = "ab\0c" doAssert a2.len == 2 # \0 is a null terminator, even in js vm
-
func len(x: string): int {.magic: "LengthStr", ...raises: [], tags: [], forbids: [].}
-
Returns the length of a string.
Example:
Source Editassert "abc".len == 3 assert "".len == 0 assert string.default.len == 0
-
func len[T](x: seq[T]): int {.magic: "LengthSeq", ...raises: [], tags: [], forbids: [].}
-
Returns the length of
x
.Example:
Source Editassert @[0, 1].len == 2 assert seq[int].default.len == 0 assert newSeq[int](3).len == 3 let s = newSeqOfCap[int](3) assert s.len == 0
-
func len[T](x: set[T]): int {.magic: "Card", ...raises: [], tags: [], forbids: [].}
-
An alias for
card(x)
. Source Edit -
proc locals(): RootObj {.magic: "Plugin", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Generates a tuple constructor expression listing all the local variables in the current scope.
This is quite fast as it does not rely on any debug or runtime information. Note that in contrast to what the official signature says, the return type is not
RootObj
but a tuple of a structure that depends on the current scope. Example:
Source Editproc testLocals() = var a = "something" b = 4 c = locals() d = "super!" b = 1 for name, value in fieldPairs(c): echo "name ", name, " with value ", value echo "B is ", b # -> name a with value something # -> name b with value 4 # -> B is 1
-
proc low(x: cstring): int {.magic: "Low", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns the lowest possible index of a compatible string
x
.See also:
Source Edit -
proc low(x: string): int {.magic: "Low", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns the lowest possible index of a string
x
.var str = "Hello world!" low(str) # => 0
See also:
Source Edit -
proc low[I, T](x: array[I, T]): I {.magic: "Low", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns the lowest possible index of an array
x
.For empty arrays, the return type is
int
.var arr = [1, 2, 3, 4, 5, 6, 7] low(arr) # => 0 for i in low(arr)..high(arr): echo arr[i]
See also:
Source Edit -
proc low[I, T](x: typedesc[array[I, T]]): I {.magic: "Low", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns the lowest possible index of an array type.
For empty arrays, the return type is
int
.low(array[7, int]) # => 0
See also:
Source Edit -
proc low[T: Ordinal | enum | range](x: T): T {.magic: "Low", noSideEffect, ...deprecated: "Deprecated since v1.4; there should not be `low(value)`. Use `low(type)`.", raises: [], tags: [], forbids: [].}
-
Returns the lowest possible value of an ordinal value
x
. As a special semantic rule,x
may also be a type identifier.This proc is deprecated, use this one instead:
Source Editlow(2) # => -9223372036854775808
-
proc low[T: Ordinal | enum | range](x: typedesc[T]): T {.magic: "Low", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns the lowest possible value of an ordinal or enum type.
low(int)
is Nim's way of writing INT_MIN or MIN_INT.low(int) # => -9223372036854775808
See also:
Source Edit -
proc max(x, y: float32): float32 {.noSideEffect, inline, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc max(x, y: float64): float64 {.noSideEffect, inline, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc max(x, y: int): int {.magic: "MaxI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc max(x, y: int8): int8 {.magic: "MaxI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc max(x, y: int16): int16 {.magic: "MaxI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc max(x, y: int32): int32 {.magic: "MaxI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc max(x, y: int64): int64 {.magic: "MaxI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- The maximum value of two integers. Source Edit
-
proc min(x, y: float32): float32 {.noSideEffect, inline, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc min(x, y: float64): float64 {.noSideEffect, inline, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc min(x, y: int): int {.magic: "MinI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc min(x, y: int8): int8 {.magic: "MinI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc min(x, y: int16): int16 {.magic: "MinI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc min(x, y: int32): int32 {.magic: "MinI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc min(x, y: int64): int64 {.magic: "MinI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- The minimum value of two integers. Source Edit
-
proc `mod`(x, y: int): int {.magic: "ModI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Computes the integer modulo operation (remainder).
This is the same as
x - (x div y) * y
.Example:
Source Editassert (7 mod 5) == 2 assert (-7 mod 5) == -2 assert (7 mod -5) == 2 assert (-7 mod -5) == -2
-
proc `mod`(x, y: int8): int8 {.magic: "ModI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `mod`(x, y: int16): int16 {.magic: "ModI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `mod`(x, y: int32): int32 {.magic: "ModI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `mod`(x, y: int64): int64 {.magic: "ModI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `mod`(x, y: uint): uint {.magic: "ModU", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Computes the integer modulo operation (remainder) for unsigned integers. This is the same as
x - (x div y) * y
. Source Edit -
proc `mod`(x, y: uint8): uint8 {.magic: "ModU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `mod`(x, y: uint16): uint16 {.magic: "ModU", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc moveMem(dest, source: pointer; size: Natural) {.inline, ...gcsafe, tags: [], raises: [], forbids: [].}
-
Copies the contents from the memory at
source
to the memory atdest
.Exactly
Source Editsize
bytes will be copied. The memory regions may overlap,moveMem
handles this case appropriately and is thus somewhat more safe thancopyMem
. Like any procedure dealing with raw memory this is still unsafe, though. -
proc new(t: typedesc): auto
-
Creates a new object of type
T
and returns a safe (traced) reference to it as result value.When
Source EditT
is a ref type then the resulting type will beT
, otherwise it will beref T
. -
proc new[T](a: var ref T) {.magic: "New", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Creates a new object of type
T
and returns a safe (traced) reference to it ina
. Source Edit -
proc new[T](a: var ref T; finalizer: proc (x: ref T) {.nimcall.}) {. magic: "NewFinalize", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Creates a new object of type
T
and returns a safe (traced) reference to it ina
.When the garbage collector frees the object,
finalizer
is called. Thefinalizer
may not keep a reference to the object pointed to byx
. Thefinalizer
cannot prevent the GC from freeing the object.Note: The
Source Editfinalizer
refers to the typeT
, not to the object! This means that for each object of typeT
the finalizer will be called! -
proc newSeq[T](len = 0.Natural): seq[T]
-
Creates a new sequence of type
seq[T]
with lengthlen
.Note that the sequence will be filled with zeroed entries. After the creation of the sequence you should assign entries to the sequence instead of adding them.
var inputStrings = newSeq[string](3) assert len(inputStrings) == 3 inputStrings[0] = "The fourth" inputStrings[1] = "assignment" inputStrings[2] = "would crash" #inputStrings[3] = "out of bounds"
See also:
Source Edit -
proc newSeq[T](s: var seq[T]; len: Natural) {.magic: "NewSeq", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Creates a new sequence of type
seq[T]
with lengthlen
.This is equivalent to
s = @[]; setlen(s, len)
, but more efficient since no reallocation is needed.Note that the sequence will be filled with zeroed entries. After the creation of the sequence you should assign entries to the sequence instead of adding them. Example:
Source Editvar inputStrings: seq[string] newSeq(inputStrings, 3) assert len(inputStrings) == 3 inputStrings[0] = "The fourth" inputStrings[1] = "assignment" inputStrings[2] = "would crash" #inputStrings[3] = "out of bounds"
-
proc newSeqUninitialized[T: SomeNumber](len: Natural): seq[T]
-
Creates a new sequence of type
seq[T]
with lengthlen
.Only available for numbers types. Note that the sequence will be uninitialized. After the creation of the sequence you should assign entries to the sequence instead of adding them. Example:
Source Editvar x = newSeqUninitialized[int](3) assert len(x) == 3 x[0] = 10
-
proc newString(len: Natural): string {.magic: "NewString", importc: "mnewString", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns a new string of length
len
but with uninitialized content. One needs to fill the string character after character with the index operators[i]
.This procedure exists only for optimization purposes; the same effect can be achieved with the
Source Edit&
operator or withadd
. -
proc newStringOfCap(cap: Natural): string {.magic: "NewStringOfCap", importc: "rawNewString", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns a new string of length
0
but with capacitycap
.This procedure exists only for optimization purposes; the same effect can be achieved with the
Source Edit&
operator or withadd
. -
proc `not`(a: typedesc): typedesc {.magic: "TypeTrait", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Constructs an
not
meta class. Source Edit -
proc `not`(x: bool): bool {.magic: "Not", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Boolean not; returns true if
x == false
. Source Edit -
proc `not`(x: int): int {.magic: "BitnotI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Computes the
bitwise complement
of the integerx
.Example:
Source Editassert not 0'u8 == 255 assert not 0'i8 == -1 assert not 1000'u16 == 64535 assert not 1000'i16 == -1001
-
proc `not`(x: int8): int8 {.magic: "BitnotI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `not`(x: int16): int16 {.magic: "BitnotI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `not`(x: int32): int32 {.magic: "BitnotI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `not`(x: int64): int64 {.magic: "BitnotI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `not`(x: uint): uint {.magic: "BitnotI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Computes the
bitwise complement
of the integerx
. Source Edit -
proc `not`(x: uint8): uint8 {.magic: "BitnotI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `not`(x: uint16): uint16 {.magic: "BitnotI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `of`[T, S](x: T; y: typedesc[S]): bool {.magic: "Of", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Checks if
x
is an instance ofy
.Example:
Source Edittype Base = ref object of RootObj Sub1 = ref object of Base Sub2 = ref object of Base Unrelated = ref object var base: Base = Sub1() # downcast doAssert base of Base # generates `CondTrue` (statically true) doAssert base of Sub1 doAssert base isnot Sub1 doAssert not (base of Sub2) base = Sub2() # re-assign doAssert base of Sub2 doAssert Sub2(base) != nil # upcast doAssertRaises(ObjectConversionDefect): discard Sub1(base) var sub1 = Sub1() doAssert sub1 of Base doAssert sub1.Base of Sub1 doAssert not compiles(base of Unrelated)
-
proc onThreadDestruction(handler: proc () {.closure, ...gcsafe, raises: [].}) {. ...raises: [], tags: [], forbids: [].}
-
Registers a thread local handler that is called at the thread's destruction.
A thread is destructed when the
Source Edit.thread
proc returns normally or when it raises an exception. Note that unhandled exceptions in a thread nevertheless cause the whole process to die. -
proc `or`(a, b: typedesc): typedesc {.magic: "TypeTrait", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Constructs an
or
meta class. Source Edit -
proc `or`(x, y: bool): bool {.magic: "Or", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Boolean
or
; returns true ifnot (not x and not y)
(if any of the arguments is true).Evaluation is lazy: if
Source Editx
is true,y
will not even be evaluated. -
proc `or`(x, y: int): int {.magic: "BitorI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Computes the
bitwise or
of numbersx
andy
.Example:
Source Editassert (0b0011 or 0b0101) == 0b0111 assert (0b0111 or 0b1100) == 0b1111
-
proc `or`(x, y: int8): int8 {.magic: "BitorI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `or`(x, y: int16): int16 {.magic: "BitorI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `or`(x, y: int32): int32 {.magic: "BitorI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `or`(x, y: int64): int64 {.magic: "BitorI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `or`(x, y: uint): uint {.magic: "BitorI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Computes the
bitwise or
of numbersx
andy
. Source Edit -
proc `or`(x, y: uint8): uint8 {.magic: "BitorI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `or`(x, y: uint16): uint16 {.magic: "BitorI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
func ord[T: Ordinal | enum](x: T): int {.magic: "Ord", ...raises: [], tags: [], forbids: [].}
-
Returns the internal
int
value ofx
, including for enum with holes and distinct ordinal types.Example:
Source Editassert ord('A') == 65 type Foo = enum f0 = 0, f1 = 3 assert f1.ord == 3 type Bar = distinct int assert 3.Bar.ord == 3
-
proc peek[TMsg](c: var Channel[TMsg]): int
-
Returns the current number of messages in the channel
c
.Returns -1 if the channel has been closed.
Note: This is dangerous to use as it encourages races. It's much better to use tryRecv proc instead.
Source Edit -
proc pop[T](s: var seq[T]): T {.inline, noSideEffect.}
-
Returns the last item of
s
and decreasess.len
by one. This treatss
as a stack and implements the common pop operation.Raises
IndexDefect
ifs
is empty.Example:
Source Editvar a = @[1, 3, 5, 7] let b = pop(a) assert b == 7 assert a == @[1, 3, 5]
-
proc pred[T, V: Ordinal](x: T; y: V = 1): T {.magic: "Pred", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns the
y
-th predecessor (default: 1) of the valuex
.If such a value does not exist,
OverflowDefect
is raised or a compile time error occurs.Example:
Source Editassert pred(5) == 4 assert pred(5, 3) == 2
-
proc prepareMutation(s: var string) {.inline, ...raises: [], tags: [], forbids: [].}
-
String literals (e.g. "abc", etc) in the ARC/ORC mode are "copy on write", therefore you should call
prepareMutation
before modifying the strings viaaddr
.Example: cmd: --gc:arc
Source Editvar x = "abc" var y = "defgh" prepareMutation(y) # without this, you may get a `SIGBUS` or `SIGSEGV` moveMem(addr y[0], addr x[0], x.len) assert y == "abcgh"
-
proc quit(errorcode: int = QuitSuccess) {.magic: "Exit", noreturn, ...raises: [], tags: [], forbids: [].}
-
Stops the program immediately with an exit code.
Before stopping the program the "exit procedures" are called in the opposite order they were added with addExitProc.
The proc
quit(QuitSuccess)
is called implicitly when your nim program finishes without incident for platforms where this is the expected behavior. A raised unhandled exception is equivalent to callingquit(QuitFailure)
.Note that this is a runtime call and using
quit
inside a macro won't have any compile time effect. If you need to stop the compiler inside a macro, use the error or fatal pragmas.Warning:errorcode
gets saturated when it exceeds the valid range on the specific platform. On Posix, the valid range islow(int8)..high(int8)
. On Windows, the valid range islow(int32)..high(int32)
. For instance,quit(int(0x100000000))
is equal toquit(127)
on Linux.Danger: In almost all cases, in particular in library code, prefer alternatives, e.g.Source EditdoAssert false
or raise aDefect
.quit
bypasses regular control flow in particulardefer
,try
,catch
,finally
anddestructors
, and exceptions that may have been raised by anaddExitProc
proc, as well as cleanup code in other threads. It does not call the garbage collector to free all the memory, unless anaddExitProc
proc calls GC_fullCollect. -
proc repr[T](x: T): string {.magic: "Repr", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Takes any Nim variable and returns its string representation. No trailing newline is inserted (so
echo
won't add an empty newline). Use-d:nimLegacyReprWithNewline
to revert to old behavior where newlines were added in some cases.It works even for complex data graphs with cycles. This is a great debugging tool.
Source Editvar s: seq[string] = @["test2", "test2"] var i = @[1, 2, 3, 4, 5] echo repr(s) # => 0x1055eb050[0x1055ec050"test2", 0x1055ec078"test2"] echo repr(i) # => 0x1055ed050[1, 2, 3, 4, 5]
-
proc resize[T](p: ptr T; newSize: Natural): ptr T {.inline, ...gcsafe, raises: [].}
-
Grows or shrinks a given memory block.
If
p
is nil then a new memory block is returned. In either way the block has at leastT.sizeof * newSize
bytes. IfnewSize == 0
andp
is not nilresize
callsdealloc(p)
. In other cases the block has to be freed withfree
.The allocated memory belongs to its allocating thread! Use resizeShared to reallocate from a shared heap.
Source Edit -
proc runnableExamples(rdoccmd = ""; body: untyped) {.magic: "RunnableExamples", ...raises: [], tags: [], forbids: [].}
-
A section you should use to mark runnable example code with.
- In normal debug and release builds code within a
runnableExamples
section is ignored. - The documentation generator is aware of these examples and considers them part of the
##
doc comment. As the last step of documentation generation each runnableExample is put in its own file$file_examples$i.nim
, compiled and tested. The collected examples are put into their own module to ensure the examples do not refer to non-exported symbols.
Example:
Source Editproc timesTwo*(x: int): int = ## This proc doubles a number. runnableExamples: # at module scope const exported* = 123 assert timesTwo(5) == 10 block: # at block scope defer: echo "done" runnableExamples "-d:foo -b:cpp": import std/compilesettings assert querySetting(backend) == "cpp" assert defined(foo) runnableExamples "-r:off": ## this one is only compiled import std/browsers openDefaultBrowser "https://forum.nim-lang.org/" 2 * x
- In normal debug and release builds code within a
-
proc setControlCHook(hook: proc () {.noconv.}) {....raises: [], tags: [], forbids: [].}
-
Allows you to override the behaviour of your application when CTRL+C is pressed. Only one such hook is supported. Example:
Source Editproc ctrlc() {.noconv.} = echo "Ctrl+C fired!" # do clean up stuff quit() setControlCHook(ctrlc)
-
proc setLen(s: var string; newlen: Natural) {.magic: "SetLengthStr", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Sets the length of string
s
tonewlen
.If the current length is greater than the new length,
s
will be truncated.
Source Editvar myS = "Nim is great!!" myS.setLen(3) # myS <- "Nim" echo myS, " is fantastic!!"
-
proc setLen[T](s: var seq[T]; newlen: Natural) {.magic: "SetLengthSeq", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Sets the length of seq
s
tonewlen
.T
may be any sequence type.If the current length is greater than the new length,
s
will be truncated.
Source Editvar x = @[10, 20] x.setLen(5) x[4] = 50 assert x == @[10, 20, 0, 0, 50] x.setLen(1) assert x == @[10]
-
proc setupForeignThreadGc() {....gcsafe, raises: [], tags: [], forbids: [].}
-
Call this if you registered a callback that will be run from a thread not under your control. This has a cheap thread-local guard, so the GC for this thread will only be initialized once per thread, no matter how often it is called.
This function is available only when
Source Edit--threads:on
and--tlsEmulation:off
switches are used -
proc shallowCopy[T](x: var T; y: T) {.noSideEffect, magic: "ShallowCopy", ...raises: [], tags: [], forbids: [].}
-
Use this instead of
=
for a shallow copy.The shallow copy only changes the semantics for sequences and strings (and types which contain those).
Be careful with the changed semantics though! There is a reason why the default assignment does a deep copy of sequences and strings.
Source Edit -
proc `shl`(x: int8; y: SomeInteger): int8 {.magic: "ShlI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `shl`(x: int16; y: SomeInteger): int16 {.magic: "ShlI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `shl`(x: int32; y: SomeInteger): int32 {.magic: "ShlI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `shl`(x: int64; y: SomeInteger): int64 {.magic: "ShlI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `shl`(x: int; y: SomeInteger): int {.magic: "ShlI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Computes the
shift left
operation ofx
andy
.Note: Operator precedence is different than in C.
Example:
Source Editassert 1'i32 shl 4 == 0x0000_0010 assert 1'i64 shl 4 == 0x0000_0000_0000_0010
-
proc `shl`(x: uint8; y: SomeInteger): uint8 {.magic: "ShlI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `shl`(x: uint16; y: SomeInteger): uint16 {.magic: "ShlI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `shl`(x: uint32; y: SomeInteger): uint32 {.magic: "ShlI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `shr`(x: int8; y: SomeInteger): int8 {.magic: "AshrI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `shr`(x: int16; y: SomeInteger): int16 {.magic: "AshrI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `shr`(x: int32; y: SomeInteger): int32 {.magic: "AshrI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `shr`(x: int64; y: SomeInteger): int64 {.magic: "AshrI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `shr`(x: int; y: SomeInteger): int {.magic: "AshrI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Computes the
shift right
operation ofx
andy
, filling vacant bit positions with the sign bit.Note: Operator precedence is different than in C.
See also:
- ashr func for arithmetic shift right
Example:
Source Editassert 0b0001_0000'i8 shr 2 == 0b0000_0100'i8 assert 0b0000_0001'i8 shr 1 == 0b0000_0000'i8 assert 0b1000_0000'i8 shr 4 == 0b1111_1000'i8 assert -1 shr 5 == -1 assert 1 shr 5 == 0 assert 16 shr 2 == 4 assert -16 shr 2 == -4
-
proc `shr`(x: uint8; y: SomeInteger): uint8 {.magic: "ShrI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `shr`(x: uint16; y: SomeInteger): uint16 {.magic: "ShrI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `shr`(x: uint32; y: SomeInteger): uint32 {.magic: "ShrI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc sizeof(x: typedesc): int {.magic: "SizeOf", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc sizeof[T](x: T): int {.magic: "SizeOf", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns the size of
x
in bytes.Since this is a low-level proc, its usage is discouraged - using new for the most cases suffices that one never needs to know
x
's size.As a special semantic rule,
x
may also be a type identifier (sizeof(int)
is valid).Limitations: If used for types that are imported from C or C++, sizeof should fallback to the
sizeof
in the C compiler. The result isn't available for the Nim compiler and therefore can't be used inside of macros.
Source Editsizeof('A') # => 1 sizeof(2) # => 8
-
proc slurp(filename: string): string {.magic: "Slurp", ...raises: [], tags: [], forbids: [].}
- This is an alias for staticRead. Source Edit
-
proc staticExec(command: string; input = ""; cache = ""): string {. magic: "StaticExec", ...raises: [], tags: [], forbids: [].}
-
Executes an external process at compile-time and returns its text output (stdout + stderr).
If
input
is not an empty string, it will be passed as a standard input to the executed program.const buildInfo = "Revision " & staticExec("git rev-parse HEAD") & "\nCompiled on " & staticExec("uname -v")
gorge is an alias for
staticExec
.Note that you can use this proc inside a pragma like passc or passl.
If
cache
is not empty, the results ofstaticExec
are cached within thenimcache
directory. Use--forceBuild
to get rid of this caching behaviour then.command & input & cache
(the concatenated string) is used to determine whether the entry in the cache is still valid. You can use versioning information forcache
:
Source Editconst stateMachine = staticExec("dfaoptimizer", "input", "0.8.0")
-
proc staticRead(filename: string): string {.magic: "Slurp", ...raises: [], tags: [], forbids: [].}
-
Compile-time readFile proc for easy resource embedding:
The maximum file size limit that
staticRead
andslurp
can read is near or equal to the free memory of the device you are using to compile.const myResource = staticRead"mydatafile.bin"
slurp is an alias for
Source EditstaticRead
. -
proc substr(s: openArray[char]): string {....raises: [], tags: [], forbids: [].}
-
Copies a slice of
s
into a new string and returns this new string.Example:
Source Editlet a = "abcdefgh" assert a.substr(2, 5) == "cdef" assert a.substr(2) == "cdefgh" assert a.substr(5, 99) == "fgh"
-
proc substr(s: string; first, last: int): string {....raises: [], tags: [], forbids: [].}
-
Copies a slice of
s
into a new string and returns this new string.The bounds
first
andlast
denote the indices of the first and last characters that shall be copied. Iflast
is omitted, it is treated ashigh(s)
. Iflast >= s.len
,s.len
is used instead: This meanssubstr
can also be used to cut or limit a string's length.Example:
Source Editlet a = "abcdefgh" assert a.substr(2, 5) == "cdef" assert a.substr(2) == "cdefgh" assert a.substr(5, 99) == "fgh"
-
proc succ[T, V: Ordinal](x: T; y: V = 1): T {.magic: "Succ", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Returns the
y
-th successor (default: 1) of the valuex
.If such a value does not exist,
OverflowDefect
is raised or a compile time error occurs.Example:
Source Editassert succ(5) == 6 assert succ(5, 3) == 8
-
proc tearDownForeignThreadGc() {....gcsafe, raises: [], tags: [], forbids: [].}
-
Call this to tear down the GC, previously initialized by
setupForeignThreadGc
. If GC has not been previously initialized, or has already been torn down, the call does nothing.This function is available only when
Source Edit--threads:on
and--tlsEmulation:off
switches are used -
proc toFloat(i: int): float {.noSideEffect, inline, ...raises: [], tags: [], forbids: [].}
-
Converts an integer
i
into afloat
. Same asfloat(i)
.If the conversion fails,
ValueError
is raised. However, on most platforms the conversion cannot fail.
Source Editlet a = 2 b = 3.7 echo a.toFloat + b # => 5.7
-
proc toInt(f: float): int {.noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Converts a floating point number
f
into anint
.Conversion rounds
f
half away from 0, see Round half away from zero, as opposed to a type conversion which rounds towards zero.Note that some floating point numbers (e.g. infinity or even 1e19) cannot be accurately converted.
Source EditdoAssert toInt(0.49) == 0 doAssert toInt(0.5) == 1 doAssert toInt(-0.5) == -1 # rounding is symmetrical
-
proc toOpenArray(x: cstring; first, last: int): openArray[char] {. magic: "Slice", ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc toOpenArray(x: string; first, last: int): openArray[char] {.magic: "Slice", ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc toOpenArray[I, T](x: array[I, T]; first, last: I): openArray[T] {. magic: "Slice", ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc toOpenArray[T](x: openArray[T]; first, last: int): openArray[T] {. magic: "Slice", ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc toOpenArrayByte(x: cstring; first, last: int): openArray[byte] {. magic: "Slice", ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc toOpenArrayByte(x: openArray[char]; first, last: int): openArray[byte] {. magic: "Slice", ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc typeof(x: untyped; mode = typeOfIter): typedesc {.magic: "TypeOf", noSideEffect, compileTime, ...raises: [], tags: [], forbids: [].}
-
Builtin
typeof
operation for accessing the type of an expression. Since version 0.20.0.Example:
Source Editproc myFoo(): float = 0.0 iterator myFoo(): string = yield "abc" iterator myFoo2(): string = yield "abc" iterator myFoo3(): string {.closure.} = yield "abc" doAssert type(myFoo()) is string doAssert typeof(myFoo()) is string doAssert typeof(myFoo(), typeOfIter) is string doAssert typeof(myFoo3) is iterator doAssert typeof(myFoo(), typeOfProc) is float doAssert typeof(0.0, typeOfProc) is float doAssert typeof(myFoo3, typeOfProc) is iterator doAssert not compiles(typeof(myFoo2(), typeOfProc)) # this would give: Error: attempting to call routine: 'myFoo2' # since `typeOfProc` expects a typed expression and `myFoo2()` can # only be used in a `for` context.
-
proc unsafeNew[T](a: var ref T; size: Natural) {.magic: "New", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Creates a new object of type
T
and returns a safe (traced) reference to it ina
.This is unsafe as it allocates an object of the passed
size
. This should only be used for optimization purposes when you know what you're doing!See also:
Source Edit -
proc `xor`(x, y: bool): bool {.magic: "Xor", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Boolean
exclusive or
; returns true ifx != y
(if either argument is true while the other is false). Source Edit -
proc `xor`(x, y: int): int {.magic: "BitxorI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Computes the
bitwise xor
of numbersx
andy
.Example:
Source Editassert (0b0011 xor 0b0101) == 0b0110 assert (0b0111 xor 0b1100) == 0b1011
-
proc `xor`(x, y: int8): int8 {.magic: "BitxorI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `xor`(x, y: int16): int16 {.magic: "BitxorI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `xor`(x, y: int32): int32 {.magic: "BitxorI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `xor`(x, y: int64): int64 {.magic: "BitxorI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `xor`(x, y: uint): uint {.magic: "BitxorI", noSideEffect, ...raises: [], tags: [], forbids: [].}
-
Computes the
bitwise xor
of numbersx
andy
. Source Edit -
proc `xor`(x, y: uint8): uint8 {.magic: "BitxorI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
-
proc `xor`(x, y: uint16): uint16 {.magic: "BitxorI", noSideEffect, ...raises: [], tags: [], forbids: [].}
- Source Edit
Iterators
-
iterator `..`(a, b: int32): int32 {.inline, ...raises: [], tags: [], forbids: [].}
-
A type specialized version of
..
for convenience so that mixing integer types works better.See also:
Source Edit -
iterator `..`(a, b: int64): int64 {.inline, ...raises: [], tags: [], forbids: [].}
-
A type specialized version of
..
for convenience so that mixing integer types works better.See also:
Source Edit -
iterator `..`(a, b: uint32): uint32 {.inline, ...raises: [], tags: [], forbids: [].}
-
A type specialized version of
..
for convenience so that mixing integer types works better.See also:
Source Edit -
iterator `..<`(a, b: int32): int32 {.inline, ...raises: [], tags: [], forbids: [].}
-
A type specialized version of
..<
for convenience so that mixing integer types works better. Source Edit -
iterator `..<`(a, b: int64): int64 {.inline, ...raises: [], tags: [], forbids: [].}
-
A type specialized version of
..<
for convenience so that mixing integer types works better. Source Edit -
iterator `..<`(a, b: uint32): uint32 {.inline, ...raises: [], tags: [], forbids: [].}
-
A type specialized version of
..<
for convenience so that mixing integer types works better. Source Edit -
iterator countdown[T](a, b: T; step: Positive = 1): T {.inline.}
-
Counts from ordinal value
a
down tob
(inclusive) with the given step count.T
may be any ordinal type,step
may only be positive.Note: This fails to count to
low(int)
if T = int for efficiency reasons.Example:
Source Editimport std/sugar let x = collect(newSeq): for i in countdown(7, 3): i assert x == @[7, 6, 5, 4, 3] let y = collect(newseq): for i in countdown(9, 2, 3): i assert y == @[9, 6, 3]
-
iterator countup[T](a, b: T; step: Positive = 1): T {.inline.}
-
Counts from ordinal value
a
tob
(inclusive) with the given step count.T
may be any ordinal type,step
may only be positive.Note: This fails to count to
high(int)
if T = int for efficiency reasons.Example:
Source Editimport std/sugar let x = collect(newSeq): for i in countup(3, 7): i assert x == @[3, 4, 5, 6, 7] let y = collect(newseq): for i in countup(2, 9, 3): i assert y == @[2, 5, 8]
-
iterator `||`[S, T](a: S; b: T; annotation: static string = "parallel for"): T {. inline, magic: "OmpParFor", sideEffect, ...raises: [], tags: [], forbids: [].}
-
OpenMP parallel loop iterator. Same as
..
but the loop may run in parallel.annotation
is an additional annotation for the code generator to use. The default annotation isparallel for
. Please refer to the OpenMP Syntax Reference for further information.Note that the compiler maps that to the
Source Edit#pragma omp parallel for
construct of OpenMP and as such isn't aware of the parallelism in your code! Be careful! Later versions of||
will get proper support by Nim's code generator and GC. -
iterator `||`[S, T](a: S; b: T; step: Positive; annotation: static string = "parallel for"): T {.inline, magic: "OmpParFor", sideEffect, ...raises: [], tags: [], forbids: [].}
-
OpenMP parallel loop iterator with stepping. Same as
countup
but the loop may run in parallel.annotation
is an additional annotation for the code generator to use. The default annotation isparallel for
. Please refer to the OpenMP Syntax Reference for further information.Note that the compiler maps that to the
Source Edit#pragma omp parallel for
construct of OpenMP and as such isn't aware of the parallelism in your code! Be careful! Later versions of||
will get proper support by Nim's code generator and GC.
Macros
Templates
-
template alloc(size: Natural): pointer
-
Allocates a new memory block with at least
size
bytes.The block has to be freed with realloc(block, 0) or dealloc(block). The block is not initialized, so reading from it before writing to it is undefined behaviour!
The allocated memory belongs to its allocating thread! Use allocShared to allocate from a shared heap.
See also:
Source Edit -
template alloc0(size: Natural): pointer
-
Allocates a new memory block with at least
size
bytes.The block has to be freed with realloc(block, 0) or dealloc(block). The block is initialized with all bytes containing zero, so it is somewhat safer than alloc.
The allocated memory belongs to its allocating thread! Use allocShared0 to allocate from a shared heap.
Source Edit -
template closureScope(body: untyped): untyped
-
Useful when creating a closure in a loop to capture local loop variables by their current iteration values.
Note: This template may not work in some cases, use capture instead.
Example:
Source Editvar myClosure : proc() # without closureScope: for i in 0 .. 5: let j = i if j == 3: myClosure = proc() = echo j myClosure() # outputs 5. `j` is changed after closure creation # with closureScope: for i in 0 .. 5: closureScope: # Everything in this scope is locked after closure creation let j = i if j == 3: myClosure = proc() = echo j myClosure() # outputs 3
-
template currentSourcePath(): string
-
Returns the full file-system path of the current source.
To get the directory containing the current source, use it with os.parentDir() as
currentSourcePath.parentDir()
.The path returned by this template is set at compile time.
See the docstring of macros.getProjectPath() for an example to see the distinction between the
currentSourcePath
andgetProjectPath
.See also:
Source Edit -
template likely(val: bool): bool
-
Hints the optimizer that
val
is likely going to be true.You can use this template to decorate a branch condition. On certain platforms this can help the processor predict better which branch is going to be run. Example:
for value in inputValues: if likely(value <= 100): process(value) else: echo "Value too big!"
On backends without branch prediction (JS and the nimscript VM), this template will not affect code execution.
Source Edit -
template realloc(p: pointer; newSize: Natural): pointer
-
Grows or shrinks a given memory block.
If
p
is nil then a new memory block is returned. In either way the block has at leastnewSize
bytes. IfnewSize == 0
andp
is not nilrealloc
callsdealloc(p)
. In other cases the block has to be freed with dealloc(block).The allocated memory belongs to its allocating thread! Use reallocShared to reallocate from a shared heap.
Source Edit -
template realloc0(p: pointer; oldSize, newSize: Natural): pointer
-
Grows or shrinks a given memory block.
If
p
is nil then a new memory block is returned. In either way the block has at leastnewSize
bytes. IfnewSize == 0
andp
is not nilrealloc
callsdealloc(p)
. In other cases the block has to be freed with dealloc(block).The block is initialized with all bytes containing zero, so it is somewhat safer then realloc
The allocated memory belongs to its allocating thread! Use reallocShared to reallocate from a shared heap.
Source Edit -
template unlikely(val: bool): bool
-
Hints the optimizer that
val
is likely going to be false.You can use this proc to decorate a branch condition. On certain platforms this can help the processor predict better which branch is going to be run. Example:
for value in inputValues: if unlikely(value > 100): echo "Value too big!" else: process(value)
On backends without branch prediction (JS and the nimscript VM), this template will not affect code execution.
Source Edit
Exports
- FloatOverflowDefect, ResourceExhaustedError, NilAccessError, FloatUnderflowDefect, RangeDefect, IndexDefect, FloatUnderflowError, FloatOverflowError, FloatDivByZeroDefect, FloatInvalidOpError, FloatingPointError, DivByZeroError, OutOfMemError, RangeError, IndexError, ReraiseError, IOError, DeadThreadError, WriteIOEffect, FloatDivByZeroError, ExecIOEffect, IOEffect, ReraiseDefect, ValueError, OverflowDefect, ReadIOEffect, ObjectConversionDefect, AccessViolationDefect, KeyError, ObjectAssignmentDefect, OverflowError, ArithmeticError, ArithmeticDefect, AccessViolationError, EOFError, FieldDefect, ObjectAssignmentError, StackOverflowDefect, NilAccessDefect, ObjectConversionError, FloatInexactError, FloatingPointDefect, TimeEffect, LibraryError, OSError, OutOfMemDefect, DivByZeroDefect, FloatInexactDefect, StackOverflowError, AssertionError, AssertionDefect, FieldError, DeadThreadDefect, FloatInvalidOpDefect, BiggestUInt, BiggestInt, cfloat, cushort, csize_t, culonglong, cuint, cshort, clonglong, clong, cuchar, PFloat64, PInt64, PInt32, culong, cschar, BiggestFloat, cstringArray, cchar, cdouble, clongdouble, cint, ByteAddress, PFloat32, atomicTestAndSet, atomicAddFetch, atomicLoadN, atomicAndFetch, atomicAlwaysLockFree, atomicXorFetch, AtomMemModel, atomicFetchAnd, atomicLoad, atomicExchange, atomicCompareExchangeN, ATOMIC_ACQUIRE, atomicSignalFence, atomicFetchAdd, cas, atomicFetchNand, cpuRelax, atomicInc, atomicOrFetch, atomicFetchSub, ATOMIC_ACQ_REL, atomicFetchXor, atomicCompareExchange, ATOMIC_RELEASE, ATOMIC_RELAXED, ATOMIC_SEQ_CST, atomicIsLockFree, atomicClear, atomicStoreN, ATOMIC_CONSUME, atomicSubFetch, atomicDec, atomicNandFetch, AtomType, atomicStore, atomicFetchOr, atomicThreadFence, fence, atomicExchangeN, doAssertRaises, raiseAssert, failedAssertImpl, doAssert, assert, onFailedAssert, items, mpairs, items, fieldPairs, items, pairs, fieldPairs, mitems, fields, mpairs, pairs, mitems, items, items, fields, mitems, items, mpairs, mpairs, items, mpairs, pairs, pairs, items, mitems, items, pairs, mitems, $, $, $, $, $, $, addFloat, $, $, $, $, $, $, $, $, $, $, $, $, $, $, getThreadId, createThread, handle, running, joinThread, createThread, joinThreads, Thread, pinToCpu, addInt, addInt, addInt, len, $, newWideCString, WideCStringObj, Utf16Char, $, newWideCString, newWideCString, WideCString, newWideCString, writeFile, write, File, write, writeChars, endOfFile, getFilePos, &=, readChars, write, readLine, open, writeFile, write, readChar, writeBuffer, readBytes, getFileHandle, close, write, getOsFileHandle, readFile, setFilePos, write, setStdIoUnbuffered, readChars, lines, stdout, readLines, getFileSize, FileHandle, write, reopen, stdmsg, writeLine, write, setInheritable, readLine, open, flushFile, readLines, readAll, FileMode, write, readBuffer, stderr, FileSeekPos, stdin, open, writeBytes, lines
© 2006–2024 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/system.html