algebraic data types and traits
TLDR
- algebraic data types and catchall for nims type system
links
- [type classes](https://nim-lang.org/docs/manual.html#generics-type-classes
- implicit generics
- type bound operators
- object variants
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
Vars
Procs
proc fieldsPrint[T: distinct tuple | object](first, second: T)
- Source Edit
proc printFields[T: RecordType](rec: T)
- Source Edit
Templates
template declareVariableWithType(T: typedesc; value: T:type)
- Source Edit