src/bookofnim/helloworld/modules/routines

Source   Edit  

routines

bookmark

TLDR

  • routine: a symbol of kind proc, func, method, iterator, macro, template, converter
    • IMO this definition should also include tasks
    • where to find which
      • converters in globalVariables
      • iterators in loopsIterators
      • lambdas in sugar
      • std & nimscript tasks in packaging
      • templates in templateMacros
      • other routine types are in this file
    • routine exception tracking is in pragmasEffects

gotchas

links

TODOs

procedures

  • returning things: (cant contain a yield statement)
    • use return keyword for early returns
    • result = sumExpression: enables return value optimization & copy elision
    • if return/result isnt used last expression's value is returned
  • overload: redeclare with different signature
  • args passed to procs are eagerly evaluated
    • see templates for lazy evaluation

openArray

  • openArrayT implemented as a pointer to the array data and a length field
  • only used in proc signatures for accepting an array of any length
    • cant be used to with multidimensional array arguments
  • always index with int and starting at 0
  • array args must match the param base type, index type is ignored
  • arrays and seqs are implicity converted for openArray params

varargs

  • enables passing a variable number of args to a proc param
  • the args are converted to an array if the param is the last param

funcs

  • alias for {. noSideEffect .}
  • compiler throws error if reading/writing to global variables
    • i.e. any var not a parameter/local
  • allocating a seq/string does not throw an err

closures

  • can be created with proc expressions or do notation

anonymous procs

  • dont have a name and surrounded by paranthesis

Vars

capitals: array[3, string]
Source   Edit  
fruits: seq[string]
Source   Edit  
gg = 0
Source   Edit  
num7 = 5
Source   Edit  
zz = "who"
Source   Edit  

Lets

xx = "I"
Source   Edit  

Procs

proc `***`(i: int): auto {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc add5(num: int): int {.noSideEffect, ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc allInts(x, y, z: int): int {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc bye(name: string): string {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc communicate(greeting: proc (x: string): string; name: string) {.
    ...raises: [Exception], tags: [RootEffect], forbids: [].}
Source   Edit  
proc copyThenMutateValue(x: string): void {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc eko(s = "Default value"): void {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc ekoAll(s: varargs[string]) {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc ekoAnything(s: varargs[string, `$`]) {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc ekoGroups(a, b: int; c: string; d: char): void {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc greet(name: string): string {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc mutate(this: var int): int {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc openArraySize(oa: openArray[string]): int {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc passedByReference(yy: var string): void {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc passedByValue(x: string): void {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc pubfn(): void {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc redurn(this: string): string {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc runFn(a: string; fn: proc (x: string): string): string {.
    ...raises: [Exception], tags: [RootEffect], forbids: [].}
Source   Edit  
proc str(s: string): string {....raises: [], tags: [], forbids: [].}
Source   Edit  
func woop(): string {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc writeAccessToG(): var int {....raises: [], tags: [], forbids: [].}
Source   Edit