cleanup Ordinal (#13501)
This commit is contained in:
parent
42dad3a836
commit
6a0e87eb38
7 changed files with 36 additions and 10 deletions
|
|
@ -104,6 +104,7 @@ proc initDefines*(symbols: StringTableRef) =
|
||||||
defineSymbol("nimHasCursor")
|
defineSymbol("nimHasCursor")
|
||||||
defineSymbol("nimHasExceptionsQuery")
|
defineSymbol("nimHasExceptionsQuery")
|
||||||
defineSymbol("nimHasIsNamedTuple")
|
defineSymbol("nimHasIsNamedTuple")
|
||||||
|
defineSymbol("nimHashOrdinalFixed")
|
||||||
|
|
||||||
when defined(nimHasLibFFI):
|
when defined(nimHasLibFFI):
|
||||||
# Renaming as we can't conflate input vs output define flags; e.g. this
|
# Renaming as we can't conflate input vs output define flags; e.g. this
|
||||||
|
|
|
||||||
|
|
@ -112,7 +112,7 @@ proc hash*[T: proc](x: T): Hash {.inline.} =
|
||||||
else:
|
else:
|
||||||
result = hash(pointer(x))
|
result = hash(pointer(x))
|
||||||
|
|
||||||
proc hash*(x: int|int64|uint|uint64|char|Ordinal): Hash {.inline.} =
|
proc hash*[T: Ordinal](x: T): Hash {.inline.} =
|
||||||
## Efficient hashing of integers.
|
## Efficient hashing of integers.
|
||||||
cast[Hash](ord(x))
|
cast[Hash](ord(x))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -87,6 +87,18 @@ proc defined*(x: untyped): bool {.magic: "Defined", noSideEffect, compileTime.}
|
||||||
## # Do here programmer friendly expensive sanity checks.
|
## # Do here programmer friendly expensive sanity checks.
|
||||||
## # Put here the normal code
|
## # Put here the normal code
|
||||||
|
|
||||||
|
when defined(nimHashOrdinalFixed):
|
||||||
|
type
|
||||||
|
Ordinal*[T] {.magic: Ordinal.} ## Generic ordinal type. Includes integer,
|
||||||
|
## bool, character, and enumeration types
|
||||||
|
## as well as their subtypes. See also
|
||||||
|
## `SomeOrdinal`.
|
||||||
|
else:
|
||||||
|
# bootstrap <= 0.20.0
|
||||||
|
type
|
||||||
|
OrdinalImpl[T] {.magic: Ordinal.}
|
||||||
|
Ordinal* = OrdinalImpl | uint | uint64
|
||||||
|
|
||||||
when defined(nimHasRunnableExamples):
|
when defined(nimHasRunnableExamples):
|
||||||
proc runnableExamples*(body: untyped) {.magic: "RunnableExamples".}
|
proc runnableExamples*(body: untyped) {.magic: "RunnableExamples".}
|
||||||
## A section you should use to mark `runnable example`:idx: code with.
|
## A section you should use to mark `runnable example`:idx: code with.
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ proc pred*[T: Ordinal](x: T, y = 1): T {.magic: "Pred", noSideEffect.}
|
||||||
## echo pred(5) # => 4
|
## echo pred(5) # => 4
|
||||||
## echo pred(5, 3) # => 2
|
## echo pred(5, 3) # => 2
|
||||||
|
|
||||||
proc inc*[T: Ordinal|uint|uint64](x: var T, y = 1) {.magic: "Inc", noSideEffect.}
|
proc inc*[T: Ordinal](x: var T, y = 1) {.magic: "Inc", noSideEffect.}
|
||||||
## Increments the ordinal ``x`` by ``y``.
|
## Increments the ordinal ``x`` by ``y``.
|
||||||
##
|
##
|
||||||
## If such a value does not exist, ``OverflowError`` is raised or a compile
|
## If such a value does not exist, ``OverflowError`` is raised or a compile
|
||||||
|
|
@ -33,7 +33,7 @@ proc inc*[T: Ordinal|uint|uint64](x: var T, y = 1) {.magic: "Inc", noSideEffect.
|
||||||
## inc(i) # i <- 3
|
## inc(i) # i <- 3
|
||||||
## inc(i, 3) # i <- 6
|
## inc(i, 3) # i <- 6
|
||||||
|
|
||||||
proc dec*[T: Ordinal|uint|uint64](x: var T, y = 1) {.magic: "Dec", noSideEffect.}
|
proc dec*[T: Ordinal](x: var T, y = 1) {.magic: "Dec", noSideEffect.}
|
||||||
## Decrements the ordinal ``x`` by ``y``.
|
## Decrements the ordinal ``x`` by ``y``.
|
||||||
##
|
##
|
||||||
## If such a value does not exist, ``OverflowError`` is raised or a compile
|
## If such a value does not exist, ``OverflowError`` is raised or a compile
|
||||||
|
|
|
||||||
|
|
@ -20,10 +20,6 @@ const
|
||||||
off* = false ## Alias for ``false``.
|
off* = false ## Alias for ``false``.
|
||||||
|
|
||||||
type
|
type
|
||||||
Ordinal*[T] {.magic: Ordinal.} ## Generic ordinal type. Includes integer,
|
|
||||||
## bool, character, and enumeration types
|
|
||||||
## as well as their subtypes.
|
|
||||||
|
|
||||||
SomeSignedInt* = int|int8|int16|int32|int64
|
SomeSignedInt* = int|int8|int16|int32|int64
|
||||||
## Type class matching all signed integer types.
|
## Type class matching all signed integer types.
|
||||||
|
|
||||||
|
|
@ -35,7 +31,7 @@ type
|
||||||
|
|
||||||
SomeOrdinal* = int|int8|int16|int32|int64|bool|enum|uint|uint8|uint16|uint32|uint64
|
SomeOrdinal* = int|int8|int16|int32|int64|bool|enum|uint|uint8|uint16|uint32|uint64
|
||||||
## Type class matching all ordinal types; however this includes enums with
|
## Type class matching all ordinal types; however this includes enums with
|
||||||
## holes.
|
## holes. See also `Ordinal`
|
||||||
|
|
||||||
BiggestInt* = int64
|
BiggestInt* = int64
|
||||||
## is an alias for the biggest signed integer type the Nim compiler
|
## is an alias for the biggest signed integer type the Nim compiler
|
||||||
|
|
|
||||||
|
|
@ -193,3 +193,20 @@ block:
|
||||||
a = {k1}
|
a = {k1}
|
||||||
b = {k1,k2}
|
b = {k1,k2}
|
||||||
doAssert a < b
|
doAssert a < b
|
||||||
|
|
||||||
|
|
||||||
|
block: # Ordinal
|
||||||
|
doAssert int is Ordinal
|
||||||
|
doAssert uint is Ordinal
|
||||||
|
doAssert int64 is Ordinal
|
||||||
|
doAssert uint64 is Ordinal
|
||||||
|
doAssert char is Ordinal
|
||||||
|
type Foo = enum k1, k2
|
||||||
|
doAssert Foo is Ordinal
|
||||||
|
doAssert Foo is SomeOrdinal
|
||||||
|
doAssert enum is SomeOrdinal
|
||||||
|
|
||||||
|
# these fail:
|
||||||
|
# doAssert enum is Ordinal # fails
|
||||||
|
# doAssert Ordinal is SomeOrdinal
|
||||||
|
# doAssert SomeOrdinal is Ordinal
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,9 @@ discard """
|
||||||
line: 17
|
line: 17
|
||||||
nimout: '''type mismatch: got <int>
|
nimout: '''type mismatch: got <int>
|
||||||
but expected one of:
|
but expected one of:
|
||||||
proc inc[T: Ordinal | uint | uint64](x: var T; y = 1)
|
proc inc[T: Ordinal](x: var T; y = 1)
|
||||||
first type mismatch at position: 1
|
first type mismatch at position: 1
|
||||||
required type for x: var T: Ordinal or uint or uint64
|
required type for x: var T: Ordinal
|
||||||
but expression 'i' is immutable, not 'var'
|
but expression 'i' is immutable, not 'var'
|
||||||
|
|
||||||
expression: inc i
|
expression: inc i
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue