Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] add isDefault; more general than isNil, isEmpty etc #13526

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@


```
- Added `system.isDefault(a)` to tell whether `a` is equal to its default value


## Library changes
Expand Down
64 changes: 55 additions & 9 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,24 @@ let nimvm* {.magic: "Nimvm", compileTime.}: bool = false
include "system/arithmetics"
include "system/comparisons"

const
NimMajor* {.intdefine.}: int = 1
## is the major number of Nim's version.

NimMinor* {.intdefine.}: int = 1
## is the minor number of Nim's version.

NimPatch* {.intdefine.}: int = 1
## is the patch number of Nim's version.

template since2(version, body: untyped) {.dirty.} =
## poor-man's version of inclrtl.since without `>=` defined
const
v0 = version[0]
v1 = version[1]
when NimMajor > v0 or NimMajor == v0 and NimMinor >= v1:
body

const
appType* {.magic: "AppType"}: string = ""
## A string that describes the application type. Possible values:
Expand Down Expand Up @@ -909,6 +927,43 @@ else:
else:
proc reset*[T](obj: var T) {.magic: "Reset", noSideEffect.}

since2 (1, 1):
template isDefault*[T](a: T): bool =
## returns whether `a` is equal to its default value
runnableExamples:
doAssert "".isDefault
doAssert not "a".isDefault
doAssert not @[0].isDefault

## semantic differences with something like isEmpty:
doAssert [0].isDefault # whereas [0].len != 0
doAssert not "".cstring.isDefault # whereas "".cstring.len == 0

type Kind = enum kUnknown, kRed, kGreen
doAssert kUnknown.isDefault
doAssert (-0.0).isDefault

type Foo1 = ref object
type Foo2 = object
doAssert not Foo1().isDefault
doAssert Foo1().notDefault
doAssert Foo2().isDefault

# pending https://github.com/nim-lang/Nim/issues/13527, use `default(T)`
a == default(type(a))

template isDefault*(a: string): bool =
## overloaded for efficiency
a.len == 0

template isDefault*[T](a: seq[T]): bool =
## overloaded for efficiency
a.len == 0

template notDefault*(a): untyped =
## negation of `isDefault`
not isDefault(a)

proc setLen*[T](s: var seq[T], newlen: Natural) {.
magic: "SetLengthSeq", noSideEffect.}
## Sets the length of seq `s` to `newlen`. ``T`` may be any sequence type.
Expand Down Expand Up @@ -2049,15 +2104,6 @@ import system/dollars
export dollars

const
NimMajor* {.intdefine.}: int = 1
## is the major number of Nim's version.

NimMinor* {.intdefine.}: int = 1
## is the minor number of Nim's version.

NimPatch* {.intdefine.}: int = 1
## is the patch number of Nim's version.

NimVersion*: string = $NimMajor & "." & $NimMinor & "." & $NimPatch
## is the version of Nim as a string.

Expand Down
8 changes: 4 additions & 4 deletions tests/errmsgs/twrong_at_operator.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ line: 22
nimout: '''
twrong_at_operator.nim(22, 30) Error: type mismatch: got <array[0..0, type int]>
but expected one of:
proc `@`[IDX, T](a: sink array[IDX, T]): seq[T]
first type mismatch at position: 1
required type for a: sink array[IDX, T]
but expression '[int]' is of type: array[0..0, type int]
proc `@`[T](a: openArray[T]): seq[T]
first type mismatch at position: 1
required type for a: openArray[T]
but expression '[int]' is of type: array[0..0, type int]
proc `@`[IDX, T](a: sink array[IDX, T]): seq[T]
first type mismatch at position: 1
required type for a: sink array[IDX, T]
but expression '[int]' is of type: array[0..0, type int]

expression: @[int]
'''
Expand Down