This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type BigObject = object | |
| field: ExpensiveToCopy | |
| proc complexNestedAccessors(o: var Obj) : var ExpensiveToCopy = | |
| var localVariable : BigObject = computeFromComplexAlgorithm() | |
| return o.AggregatedObject.accessor(localVariable) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # While programming in nimrod, I often miss the reference types from c++ | |
| # Consider this example: | |
| type CompositeObject = object | |
| unwantedCopy: string | |
| expensiveToCopy: list[matrix] | |
| impossibleToCopy: map[string, TNetworkConnection] | |
| proc properAccessor(o: var CompositeObject) : var string = | |
| return o.unwantedCopy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| template HelperTemplate(a, b, c: expr) : expr = | |
| .... | |
| template AnotherHelper(x: expr, y: stmt) : stmt = | |
| macro ComplexBuilder(a, b, c, d: expr, y: stmt) : stmt = | |
| var ast1 = getAst(helperTemplate(a,b,c)) | |
| var ast2 = getAst(anotherHelper(d, y)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| proc foo(this: var TBar) = | |
| barMethod(10) # the compiler tries both this.barMethod(10) and just barMethod(10) | |
| a = 340 # the compiler tries both this.a = 340 and assignment to global a = 340 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| proc applyOperatorToTuple(tp: tuple[T...], operator: symbol) = transformedTyple #couldn't be a specific proc type | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| proc apply[OpSymbol, T: tuple](t: T) : auto = | |
| macro: | |
| var retType = newNimrodType(ntkTuple) | |
| for fields in T: | |
| retType.add(inferType(newCall(OpSymbol, T[1])) #skiping some detail | |
| var transformedExp = .. build and expression of the type (OpSymbol(T[0]), OpSymbol(T[1]), ) | |
| template: | |
| var x : retType = transformedExp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| proc foo[T](x: T) = | |
| var y = ... | |
| bar(baz) | |
| macro: | |
| var typeOfY = inferType(y) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # First, we introduce advices that modify existing methods: | |
| before foo(b: string): int = | |
| b = "input parameters could be modified" & b | |
| after foo(b: string): int = | |
| result = result & "results too" | |
| wrap foo(b: string): int = | |
| # here, we can write arbitrary code | |
| if flipCoin(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #the problem is this | |
| proc `+=`, 10, left (a: int, b: int) | |
| proc `+=`, 20, right (a: string, b: string) | |
| # what about this? | |
| operator `+=`, 10, left | |
| proc `+=` (a: int, b: int) | |
| proc `+=` (a: string, b: string) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| template repeatStmt(N: int, code: stmt) : stmt = | |
| when N > 0: | |
| code | |
| repeatStmt(N-1, code) | |
| template repeatTemplate(N: int, tmpl: expr) : stmt = | |
| when > 0: | |
| tmpl(N) | |
| repeatTemplate(N-1, tmpl) |