Skip to content

Commit

Permalink
return types must not be Natural for reasons I won't outline here
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Apr 2, 2020
1 parent d01fca9 commit 8ee0771
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/pure/collections/sequtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ template keepItIf*(varSeq: seq, pred: untyped) =
setLen(varSeq, pos)

since (1, 1):
template countIt*(s, pred: untyped): Natural =
template countIt*(s, pred: untyped): int =
## Returns a count of all the items that fulfilled the predicate.
##
## The predicate needs to be an expression using
Expand Down

3 comments on commit 8ee0771

@timotheecour
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Araq why?

when defined case1:
  proc fun(a: int): Natural = a
  echo fun(1) # ok
  echo fun(-1) # ok: correctly gives RT RangeError

when defined case2:
  # correctly gives CT Error: conversion from int literal(-1) to Natural is invalid
  proc fun2(a: int): Natural = -1
  discard fun2(1)

also, it's used in several places already:

compiler/lexer.nim:315:78:  proc matchUnderscoreChars(L: var TLexer, tok: var TToken, chars: set[char]): Natural =
lib/pure/unicode.nim:875:41:proc graphemeLen*(s: string; i: Natural): Natural =
tests/concepts/mvarconcept.nim:12:31:proc randomInt*(max: Positive): Natural =
tests/concepts/tconcepts_issues.nim:159:28:  proc nrow(dt: DataTable) : Natural =
tests/iter/titer12.nim:50:32:proc len[T](it : iterator : T) : Natural =

@Araq
Copy link
Member Author

@Araq Araq commented on 8ee0771 Apr 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var i = len(a)
while i >= 0:
  dec i # range error for Natural, works with int.

Instead the declaration should be

proc len(x): int {.ensures: result >= 0.}

@timotheecour
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.