Skip to content

Implementation of high/low for all uint types #6620

@bluenote10

Description

@bluenote10

Since uint and uint64 are not Ordinal, it is currently not possible to use high/low on them in contrast to uint8/uint16/uint32. This is unfortunate when writing generic algorithms which make use of high/low and should work on all number types. As a workaround it is currently necessary to bring some manual implementations into scope like:

proc low*[T: uint|uint64](x: typedesc[T]): T =
  cast[T](0)

proc high*[T: uint|uint64](x: typedesc[T]): T =
  cast[T](-1)

For consistency it would be nice to have them by default. Note: I'm not trying to make them Ordinals, but just because they don't have all properties of an Ordinal (I assume overflow checks), the types still have a smallest/largest value.

Also there is currently an inconsistency in high and low, because high is defined for high[T: Ordinal](x: T) while low uses no type selector, i.e., low[T](x: T). As a result, the following code

var x: uint
echo low(x)

errors with Error: invalid argument for 'low' (a local error from semLowHigh in semexprs.nim when handling the magic), while trying to use high gives the clearer type mismatch.

If desired, I can work on that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions