diff options
| author | bptato <nincsnevem662@gmail.com> | 2024-11-28 17:44:40 +0100 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2024-11-28 17:44:40 +0100 |
| commit | bd38cbbc7068737214129cc5365ee8d4bfecda9c (patch) | |
| tree | da9d6f772eea3e8def9d58c1a5afd568375b2d7a /doc/hacking.md | |
| parent | 46abcd79d5422261cc349cc0b4981357d5ae2fcd (diff) | |
| download | chawan-bd38cbbc7068737214129cc5365ee8d4bfecda9c.tar.gz | |
twtstr: add mypairs
This couldn't get into system.nim for technical reasons, but it's still
pretty useful when iterating over non-mutable openArrays.
Diffstat (limited to 'doc/hacking.md')
| -rw-r--r-- | doc/hacking.md | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/doc/hacking.md b/doc/hacking.md index 5acb6617..57000a5c 100644 --- a/doc/hacking.md +++ b/doc/hacking.md @@ -100,18 +100,25 @@ except in short accumulator-style procedures. #### Implicit initialization -Avoid, except for arrays. The correct way to create an object: +Avoid. The correct way to create an object: ```nim let myObj = MyObject( param1: x, param2: y + # if there's e.g. a param3 too, it's OK to leave it out and let it be + # default initialized. ) ``` -It's OK to leave out param3 and let it be zero-initialized. Also, -manually initializing arrays is annoying, so it's OK to do it -implicitly. +For arrays, use: + +```nim +var buf1 = default(array[1234, char]) # when you need 0-initialization +var buf2 {.noinit.}: array[1234, char] # when you don't need 0-initialization +``` + +For primitive types, just set them to 0, "", etc. #### "out" parameters @@ -119,11 +126,26 @@ They crash the 1.6.14 compiler. Use "var" for now. #### Copying operations -substr and x[n..m] copies. Try to use toOpenArray instead, which is a -non-copying slice. +`substr` and `x[n..m]` copies. Try to use `toOpenArray` instead, which +is a non-copying slice. (Obviously, you should use `substr` if you +*need* to copy.) -Note that `=` is not just assignment, it's a "copy" operator. If you're -copying a large object a lot, you may want to set its type to `ref`. +Note that `=` usually copies. If you're copying a large object a lot, +you may want to set its type to `ref`. + +Beware of `pairs` on sequences of objects; it copies. Use `mypairs` +if you don't need mutation, `mpairs` if you do: + +```nim +proc foo(objs: openArray[SomeObj]) = + for i, obj in objs: # this copies. obj is of type "SomeObj". + obj.bar(i) + # import utils/twtstr to use mypairs. + for i, obj in objs.mypairs: # doesn't copy. obj is of type "lent "SomeObj". + obj.bar(i) + for i, obj in objs.mpairs: # doesn't copy. obj is of type "var SomeObj". + obj.i = i +``` #### Generic parameters for JS values |