Skip to content

Commit

Permalink
impl list for JS
Browse files Browse the repository at this point in the history
  • Loading branch information
litlighilit committed Apr 9, 2024
1 parent ee2b96d commit ea50087
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/pylib.nim
Original file line number Diff line number Diff line change
Expand Up @@ -224,26 +224,36 @@ iterator enumerate*[T](x: Iterable[T]): (int, T) =
yield (i, v)
i.inc


template listImpl(iter, result) =
when compiles(iter.len):
result = newSeq[T](iter.len)
for i, v in enumerate(iter):
result[i] = v
else:
for i in iter:
result.add i


when defined(js):
# XXX: a workaround, at least support some types
#[ XXX: a workaround, at least support some types
when for js:
nim compiler will complain about `for i in iter`'s `i` ,
saying: internal error: expr(nkBracketExpr, tyUserTypeClassInst) ]#
func list*[T](arr: openArray[T]): seq[T] =
for i in arr:
result = newSeq[T](arr.len)
for i,v in enumerate(arr):
result[i] = v
proc list*[T](iter: not openArray[T] and Iterable[T]): seq[T] =
#listImpl iter, result
# XXX: Code above makes compiler error:
# `internal error: genTypeInfo(tyInferred)`
for i in iter:
result.add i
proc list*[T](iter: not openArray[T] and Iterable[T]): seq[T]{.error:"""
XXX: when for js:
nim compiler will complain about `for i in iter`'s `i` ,
saying: internal error: expr(nkBracketExpr, tyUserTypeClassInst)
""".} # TODO: when solved, remove workaround below.
else:
# it has side effects as it calls `items`
proc list*[T](iter: Iterable[T]): seq[T] =
when compiles(iter.len):
result = newSeq[T](iter.len)
for i, v in enumerate(iter):
result[i] = v
else:
for i in iter:
result.add i
listImpl iter, result

when defined(js):
func filter*[T](comp: NoneType | proc(arg: T): bool, iter: Iterable[T]): Filter[T]{.error: """
Expand Down

0 comments on commit ea50087

Please sign in to comment.