Let's say I have the following sequences:
var s1: seq[int] = @[]
var s2: seq[int]
var s3: seq[int] = nil
var s4: seq[int] = newSeq[int](4)
Which of these are typically considered "empty"? And what is the most idiomatic way to test if they are empty?
Right now I am just checking if len
is 0
:
proc doSomething(s: seq[int]) =
if s.len() == 0:
echo("Your sequence is empty.")
else:
# do something
s.len == 0
is pretty idiomatic. Note that it will work fornil
seqs and empty seqs. You can also check fornil
explicitly withs.isNil
. – Symphonize