src/bookofnim/helloworld/modules/variableGlobals

Source   Edit  

variables and globals

TLDR

  • catchall for global keywords/procs/types/etc not specified in other files
  • anything like BLAH= can be written BLAH =
    • the former enables you to define/overload operators via 'proc woop=bloop: doop = toot'
  • converts are listed here because their purpose is implicit type coercion
  • additional type features are covered in structuredContainers.nim
  • you can call clear on pretty much anything

links

TODOs

  • blah.reset a thing to its default value

var

  • runtime mutable global var

let

  • runtime immutable

const

  • compile-time evaluation cannot interface with C
  • there is no compile-time foreign function interface at this time.
  • consts must be initialized with a value
  • declares variables whose values are constant expressions

static

  • static represents a compile time entity, whether variable, block or type restriction
  • staticT meta type representing all values that can be evaluated at compile time
  • all static params are treated as generic params, thus may lead to executable bloat (see generics)

eval

  • eval executes a block of code at compile time

stropping

  • enable use of keywords as operators

auto

  • auto only for proc return types and signature parameters
  • parameter auto: creates an implicit generic of proc[x](a: x)
  • return auto: causes the compiler to infer the type form the routine body

type casts

  • cast operator forces the compiler to interpret a bit pattern to be of another type
  • i.e. interpret the bit pattern as another type, but dont actually convert it to the other type
  • only needed for low-level programming and are inherently unsafe

type coercions

  • aka type conversions sometimes in docs, but always means coercion
    • type coercions preserve the abstract value, but not the bit-pattern
  • allocCStringArray creates a null terminated cstringArray from
  • chr(i): convert 0..255 to a char
  • cstringArrayToSeq
  • only widening (smaller > larger) conversions are automatic/implicit
  • ord(i): convert char to an int
  • parseInt/parseFloat from a string
  • toFloat(int): convert int to a float
  • toInt(float): convert float to an int
  • toOpenArray
  • toOpenArrayByte

type inspection

  • type(x): retrieve the type of x, discouraged should use typeof
  • typeof(x, mode = typeofIter): retrieve the type of x
  • typeOfProc: retrieve the result of a proc, i.e. typeof x, typeOfProc
  • TypeofMode: enumtypeofProc|typeofIter second param to typeof

echo/repr

  • roughly equivalent to writeLine(stdout, x); flushFile(stdout)
  • available for the JavaScript target too.
  • cant be used with funcs/{.noSideEffect.} (use debugEcho)

important globals

  • nimvm: true if current execution context is compile time

Types

MyType = ref object of RootObj
Source   Edit  
Option[T] = object
  
Source   Edit  

Vars

a = "i was a"
Source   Edit  
autoInt: auto = 7
type inference; relevant for proc return types Source   Edit  
b = "i was b"
Source   Edit  
d33pcopy: string
if gc:arc|orc you have to enable via --deepcopy:on Source   Edit  
instance: MyType = MyType()
Source   Edit  
myInt = 10
Source   Edit  
othervar: string = ""
Source   Edit  
somevar: seq[char] = @['n', 'o', 'a', 'h']
Source   Edit  
woop1 = "flush"
mutable vars dont require a value when defined Source   Edit  

Lets

aa = Option[int](hasValue: true, value: 1)
Source   Edit  
bb = Option[int](hasValue: true, value: 2)
Source   Edit  
let = "stropping"
stropping enables keywords as identifiers Source   Edit  
myStaticVar = 3
static(x): force the compile-time evaluation of the given expression Source   Edit  
someString = "some string"
Source   Edit  
woop2 = "hello"
must be assigned a value Source   Edit  

Consts

fac4 = 24
use of semi colins to have multiple statements on a single line Source   Edit  
woop3 = "flush"
must be assigned a value Source   Edit  

Procs

proc doubleFloat(x: float): float {....raises: [], tags: [], forbids: [].}
Source   Edit  

Converters

converter get[T](x: Option[T]): T
create an implicit conversion for OptionT now Optionint + Optionint works Source   Edit  
converter toBool(x: int): bool {....raises: [], tags: [], forbids: [].}
Source   Edit