-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
typetraits: add rangeof(T), a shortcut for low(T)..high(T) #15232
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,3 +183,32 @@ since (1, 1): | |
|
||
type T2 = T | ||
genericParamsImpl(T2) | ||
|
||
func rangeof*(T: typedesc): Slice[T] {.inline, since: (1, 3, 5).} = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
assert 42 in sliceof(Positive): It's hard to understand that statement without knowing what slices are in Nim, and even then you'd need to whip out the docs just to know what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well it returns a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but arguably, comparing between these: assert 42 in rangeof(Positive)
assert 42 in sliceof(Positive) The former does a better job at saying "I want to make sure that 42 is within the range of type You can know |
||
## Returns a slice containing the full range of type `T`. This is a shortcut | ||
## for `low(T)..high(T)`. | ||
## | ||
## This proc is useful for verifying whether value of one type | ||
## can be safely converted to an another: | ||
## | ||
## .. code-block:: nim | ||
## :test: "$nim $backend $options" | ||
## | ||
## import strutils | ||
## | ||
## type | ||
## AllowedPort = range[1024..65535] | ||
## | ||
## proc setupServer(port: AllowedPort) = | ||
## # Setup a webserver... | ||
## discard | ||
## | ||
## stdout.write("Please enter a port [1024-65535]: ") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. example is way too complex especially for this simple API; good examples should illustrate semantics and edge case, that's it |
||
## stdout.flushFile() | ||
## let port = stdin.readLine().parseInt() | ||
## | ||
## if port in rangeof(AllowedPort): | ||
## setupServer(AllowedPort(port)) | ||
## else: | ||
## stderr.write("Invalid port number") | ||
low(T)..high(T) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I honestly think this is too short to justify it's own stdlib function, people can just write that themselves. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since when do examples go into the changelog?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a trend started by Nim 1.2 where we put examples of new syntactic sugar to the change log.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
example belongs where the API is defined, not in the changelog (DRY, plus follows current convention, plus changelog is too big already for adding such examples)