[nim-dev] Re: Proposal of new functions in stdlib

  • From: filwit <witte2008@xxxxxxxxx>
  • To: nim-dev@xxxxxxxxxxxxx
  • Date: Tue, 3 Nov 2015 00:32:00 +0000

**@cblake** +1

Also, it would be nice to also work with single objects as well, so:
```nim
proc `&=`*[T](a: var seq[T], b: T) {.inline.} = a.add(b)
proc `&=`*[T](a: var seq[T], b: openarray[T]) = ...
```

Somewhat related, there are a couple of oddities with the ``seq`` procedures in
``system``. First, why does ``len`` and ``xlen`` return different values?

```nim
var s = newSeq[int](10)
echo s.len # 10
echo s.xlen # 9
```

Second, why doesn't the ``add`` procedures follow ``len``'s example and do
nil-checks for you? It seems the ideal place to do checks since ``add``
potentially allocate memory anyways. If it checked and auto-allocated for you
(with an ``xadd`` alternative, like ``xlen``) I think it would avoid one of the
biggest new-user gotchas in Nim.

```nim
var s: seq[int]
echo s.len # prints '0'
s.add(1) # runtime exception (this should work!)

echo s.xlen # unchecked
s.xadd(2) # unchecked
```

Other related posts: