src/bookofnim/helloworld/modules/typeSimple

Source   Edit  

simple types

TLDR

  • Conversion between int and int32 or int64 must be explicit except for string literals.
  • stay away from blah% operators in practice
    • % are mainly for backwards compatibility with previous nim versions
  • generally procs that work for strings work for chars
  • generally strings can use any seq proc for manipulation
  • similar to js isNull, use blah.isNil, or blah != nil
    • however you cannot if blah not nil: ...

links

string

  • value semantics
  • are really just seqchar|byte except for the terminating nullbyte 0
  • ^0 terminated so nim strings can be converted to a cstring without a copy
  • compared using lexicographical order
  • to intrepret unicode, you need to import the unicode module
  • addQuoted escapes and quotes y then appends to x in place
  • addEscapedChar escapes y (backslashes everything) then appends to x

char

  • 8 bit char (unsigned, basically an alias for uint8)
  • always 1 byte so cant represent most UTF-8 chars
  • single ASCII characters
  • enclosed in single quotes

byte

  • alias for uint8 that is 8 bits wide
  • if dealing with binary blobs, prefer seqbyte > string,
  • if dealing with binary data, prefer seqchar|uint8
  • ByteAddress alias for int, used for converting pointers to itneger addresses

bool

  • only true & false (i.e. not 1/0) evaluate to bool
    • but its an enum, so 0=false, 1=true
    • off/on are aliases for true/false
  • if and while conditions must be of type bool

boolean procs

  • != == etc
  • is
  • isnot
  • not
  • notin
  • and
  • or
  • in
  • xor
  • of

comparisons

  • cmp two numbers/strings

string/char procs

  • 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
  • substr
  • find returns index of char in string
  • contains true/false

numbers

number procs

  • and Bitwise and &
  • ashr Arithmetic shift right
  • abs value of some number
  • div Integer division
  • high(NumberType)
  • Inf == high(int)
  • low(NumberType)
  • max(x,y)
  • min(x,y)
  • mod Integer modulo (remainder) %
  • not Bitwise not (complement) ~
  • or Bitwise or |
  • Positive range1..high(int)
  • shl Shift left <<
  • shr Shift right >>
  • toBiggestFloat
  • toBiggestInt
  • toFloat Convert an integer into a float
  • toInt Convert floating-point number into an int
  • xor Bitwise xor ^

number types

  • BackwardsIndex alias for distinct int, see range docs
  • BiggestInt alias for int64
  • BiggestUInt alias uinty64
  • float (alias for float64|BiggestFloat) === processors fastest type,
  • float32,64
  • Inf float64 infinity
  • int (signed), 32bit/64bit os arch, platform word size, bitwidth depends on os arch
  • int8,16,32,64
  • NaN not a number
  • Natural alias for range[0, ..high(int): useful for documentation/debugging/guards
  • NegInf !inf
  • PFloat32 alias for ptr float32
  • PFloat64 alias for ptr float64
  • PInt32 alias for ptr int32
  • Pint64 alias for ptrint64
  • Positive alias for range[1, ..high(int): useful for documentation/debugging/guards
  • SomeFloat matches all float types
  • SomeInteger matches SomeSigned|UnsignedInt
  • SomeNumber matches SomeInteger|Float
  • SomeSignedInt matches all signed integer types
  • SomeUnsignedInt matches all unsigned integer types
  • uint: unsigned integers, 32/64 bit depending on system,
  • uint8,16,32,64

reference types

  • ptr generic untraced pointer
  • ref generic traced pointer

reference procs

  • addr Take the address of a memory location
  • owned
  • unown
  • disarm

misc types

  • typedesc meta type denoting a type description, e.g. in a proc signature to pass in a type
  • nil
  • void
  • auto for type inference

Vars

globalfloat = 2.5
Source   Edit  
globalint = 2
Source   Edit  
globalstring = "before"
Source   Edit  
msg: string = "yolo"
Source   Edit  

Lets

char1 = 'a'
Source   Edit  
flush = r"raw string, escapes arent interpreted"
Source   Edit  
multiline = """    can be split on multiple lines,
    escape sequences arent interpreted
  """
Source   Edit  
str1 = "wooper scooper"
Source   Edit  
str2 = """    long
    string
    literal
    nothing\i\s
    \s\es\ca\pe\d
    """
Source   Edit  
str3 = r"raw string\tliteral"
Source   Edit  
str4 = "\r\rstring\t\n\rstring"
Source   Edit  
str5 = "hex \x02"
Source   Edit  
sum: int = int(x1) + int(y1) + int(z1)
Source   Edit  
woop6 = "flush\n\n\n\n\n\nescapes are interpreted"
Source   Edit  
x1: int32 = int32(1)
Source   Edit  
xxx = 'a'
Source   Edit  
y = 'm'
Source   Edit  
y1: int8 = int8('a')
Source   Edit  
z = 'y'
Source   Edit  
z1: float = 2.5
Source   Edit  

Consts

amilliamilliamilli = 1000000
Source   Edit  
b = 100
Source   Edit  
c = 100'i8
Source   Edit  
e: uint8 = 100
Source   Edit  
f = 100'u8
Source   Edit  
g = 100.0
Source   Edit  
h = 100.0'f32
Source   Edit  
i = 40000000.0
Source   Edit  
l = 1000000000.0
Source   Edit  
m = 1000000000.0
Source   Edit  
n = 0x00000123
Source   Edit  
negint = -1
Source   Edit  
num0 = 0
Source   Edit  
num1: int = 2
Source   Edit  
num2: int = 4
Source   Edit  
num3 = 2.0
Source   Edit  
num4 = 4.0'f32
Source   Edit  
num5: float64 = 4.9'f64
Source   Edit  
o = 0b00000000000000000000000001010101
Source   Edit  
p = 0o000000000123
Source   Edit