Nim Reference

Common constructs

const x = 5 Compile-time constant
let y = "Hello" Immutable binding
var z = [1, 2, 3] Mutable variable
proc name(param: int): ReturnType = body
method name(param: float): ReturnType = body
iterator items(list: seq[int]): int = body
template name(param: typed) = body
macro name(param: string): untyped = body
if x > 5:
body
elif y == "Hello":
body
else:
body
case x
of 5:
body
of 1, 2, 3: body
of 6..30:
body
for item in list:
body
for i in 0..<len(list):
body
while x == 5:
if y.len > 0:
break
else:
continue
try:
raise err
except Exception as exc:
echo(exc.msg)
finally: discard

Input/Output

echo(x, 42, "text") readFile("file.txt")
stdout.write("text") writeFile("file.txt", "contents")
stderr.write("error") open("file.txt", fmAppend)
stdin.readLine()  

Type definitions

type
  MyType = object
    field: int
type
  Colors = enum
    Red, Green,
    Blue, Purple
type
  MyRef = ref object
    field*: string

Common infix operations (highest precedence first)

* Multiplication
/ Division (returns float)
div Division (returns integer)
mod Modulus
shl Bit shift left
shr Bit shift right
% String formatting
+ Addition
- Subtraction
& Concatenation
.. Constructs a slice
== <= < >= > != not Boolean comparisons
in notin Determines whether a value is within a container
is isnot Compile-time type equivalence
of Run-time instance of type check
and Bitwise and boolean and operation

Collections

string   seq[T]   Table[T]  
"Hello World"   @[1, 2, 3, 4]   import tables initTable({"K": "V"})  
str.add("Hi")   list.add(21)   table["Key"] = 3.5  
    list.del(2) list.delete(2)   table.del("Key")  
"Hi"[0] 'H' @[1,2][0] 1 table["Key"] 3.5
"Hi"[^1] 'i' @[1,2][^1] 2 "b" in table false
"Hey"[0..1] "He" @[1,2,3][0..1] 1,2    
"Hey"[1..^1] "ey" @[1,2,3][1..^1] 2,3    
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.229.44