src/bookofnim/helloworld/modules/traitsAdt

Source   Edit  

algebraic data types and traits

TLDR

  • algebraic data types and catchall for nims type system

links

TODOs

  • read through the scala notes and try to replicate the algebraic DTs
  • move all the type logic stuff in here
  • create a test file
  • add readme
  • add to bookofnim
  • metatype examples
  • type bound operator examples (and should probably reread those docs)
  • probably should reread the typedesc docs
  • symbol lookups in generics
    • mixin statement
    • bind statement
    • delegating bind statements
  • object variants: reread the docs
    • using the dereferencing operator to reassign a case objects fields after instantiation
    • differences with case + elif branches in the case statement
    • enums vs range type for the discrimator field
  • generics and the method call syntax
    • theres a [:X] syntax that doesnt conflict with the method call syntax (blah.method)

metatypes

  • untyped lookup symbols & perform type resolution after the expression is interpreted & checked
    • i.e. expression is lazily resolved to its value (for templates)
    • use to pass a block of statements
  • typed: semantic checker evaluates and transforms args before expression is interprted & checked
    • an expression that is eagerly resolved to its value (for templates)
    • i.e. whenever u set a type in a signature its resolved immediately
  • typedesc a type description
  • void absence of any type, generally used as proc return type

type bound operators

  • a proc or func whose name starts with = but isnt an operator
  • unrelated to propertie setters which end in = despite syntax similarities
    • x =copy y
    • x =destroy y Generic destructor implementation
    • x =sink y Generic sink implementation
    • x =trace y Generic trace implementation

type classes

  • pseudo type that can be used to match via the is operator
  • object, tuple, enum, proc, ref, ptr, var, distinct, array, set, seq auto
  • in addition, every generic type creates a type class of the same name

typedesc

  • since nim treats the names of types as regular values in certain contexts in the compilation phase
  • typedesc is a generic type for all types denoting the type class of all types
  • procs using typedesc params are implicitly generic

object variants

  • preferred over an object hierarchy with multiple levels when simple variants suffice
  • are tagged unions, which use an enum to discrimate between variant
    • generally a field called kind is set to SomeEnum, whose fields determine the branch
  • also called case objects in the docs

variant pragmas

  • uncheckedAssign disables re-assignment restrictions

generics

  • abc

Types

BiggerMoney = distinct BigMoney
Source   Edit  
BiggestMoney {.borrow: `.`.} = distinct BigMoney
Source   Edit  
BigMoney = int
Source   Edit  
FakeOption = object
  
Source   Edit  
Language = ref LanguageObj
Source   Edit  
LanguageKind = enum
  typescript, nimlang, shell
Source   Edit  
LanguageObj = object
  
Source   Edit  
OtherRecord = tuple[wtf: string]
Source   Edit  
RecordType = (typeof myRecord) or object | tuple
Source   Edit  
StrOrInt = string | int
Source   Edit  

Vars

fireTeam = Language(kind: nimlang, stack: "allstack", appName: "nirvai",
                    c: zeroDefault([type node]))
Source   Edit  
ii: int
Source   Edit  
myOpt = FakeOption(key: true, val: "has a value")
Source   Edit  
myRecord: tuple[wtf: string] = (wtf: "yo")
Source   Edit  
opsTeam = Language(kind: shell, stack: "network", appName: "nirvConnect",
                   bash: zeroDefault([type node]))
Source   Edit  
utherRecord: OtherRecord = (wtf: "yo2")
Source   Edit  
webTeam = Language(kind: typescript, stack: "fullstack", appName: "nirvaiWeb",
                   bun: zeroDefault([type node]))
Source   Edit  
x`gensym0: int = 42
Source   Edit  

Lets

thisInt: StrOrInt = 1
Source   Edit  
thizString: StrOrInt = "1"
Source   Edit  

Procs

proc fieldsPrint[T: distinct tuple | object](first, second: T)
Source   Edit  
proc foo[T](i: T)
Source   Edit  
proc printFields[T: RecordType](rec: T)
Source   Edit  
proc wtf[T](a: T): auto
Source   Edit  

Templates

template declareVariableWithType(T: typedesc; value: T:type)
Source   Edit