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
Abstracting type limits (numeric and not only). #2252
base: master
Are you sure you want to change the base?
Changes from 1 commit
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 |
---|---|---|
@@ -0,0 +1,133 @@ | ||
- Feature Name: Limits trait for the rust types | ||
- Start Date: 2017-12-20 | ||
- RFC PR: | ||
- Rust Issue: | ||
|
||
# Summary | ||
This is an RFC to add a universal trait for the type limits. | ||
|
||
# Motivation | ||
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. The language in this section could be improved a bit =) 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. Unfortunately I am not an english native speaker. Could you correct me please? 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'd write it as (with some slight semantic changes and clarifications and added Haskell as an example):
|
||
The motivation is quite simple: [make an ability to accept template types with with limits by requiring a trait](https://stackoverflow.com/questions/47904954/rust-finding-the-maximum-allowable-value-for-generic-data-type-t) | ||
so that it simplifies and generalizes the code. Another motivation is that we have all that `max_value()` and `min_value()` implemented as | ||
usual methods of a type implementation, generalizing this to a trait makes the code simplier and avoids duplicating code. Also, looking at | ||
C++ template of `std::numeric_limits` tells us we must have this thing too because it is easier to use. | ||
|
||
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. Please elaborate on why this has to be in the standard library and not in a separate crate. 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. Because if we have it standardized, the standard library types should implement it as all of them already have it implicitly by providing Are these reasons enough? 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. Historically no, the same way there isn't a trait for 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. @scottmcm Do you think it should be in a separate crate? Could you tell why you think so please? 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. @vityafx Personally, I like traits for things, to be able to write stuff as generics instead of macros. But the standard library tends to be more conservative about adding traits, leaving that to the 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 think that moving this to the 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. Removing, sure, but you don't need to remove the 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 why do we need the implementations for the standard types then in a separate crate? And also, we talked about requiring |
||
# Detailed design | ||
The design is quite simple: put everything related to the limits what can be generalized into a separate trait in the standard library: | ||
|
||
```rust | ||
trait Limits<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. This should probably be 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. Good notice! |
||
fn min_value() -> T; | ||
fn max_value() -> 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. In all of your examples below, you've used constant which is known at compile time. trait Limits<T> {
const MIN_VALUE: T;
const MAX_VALUE: 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. Also.. Why a combined trait instead of one trait for 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. Nice! Don't know why I forgot we are able to do this in traits! This looks nice, but the question then: why do we have 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. My guess is that these inherent methods precede the stabilization of associated constants, but I could be wrong. 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. @Centril I don't quite understand. Do you mean that before we can have a constant, we need a method? Or simply that the associated constants feature didn't exist when the methods were introduced? 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. Also, I would prefer 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. If there is a use case for runtime-determined (non- 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. About combined trait: we may do even better: we may have 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. About function vs constants. I prefer both the flexibility and simpliness. However, neither constants nor functions have both of them:
So, the question is open, I guess. If I am wrong anywhere, correct me please. 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.
|
||
|
||
#[derive(Debug, Copy, Clone)] | ||
struct A { | ||
field: u64, | ||
} | ||
impl Limits<A> for A { | ||
fn min_value() -> A { | ||
A { | ||
field: 0u64, | ||
} | ||
} | ||
|
||
fn max_value() -> A { | ||
A { | ||
field: 5u64, | ||
} | ||
} | ||
} | ||
impl Limits<u32> for u32 { | ||
fn min_value() -> u32 { | ||
0 | ||
} | ||
fn max_value() -> u32 { | ||
10u32 | ||
} | ||
} | ||
|
||
fn get_limits<T: Limits<T> + std::fmt::Debug>(_t: T) { | ||
println!("Minimum value: {:?}", T::min_value()); | ||
println!("Maximum value: {:?}", T::max_value()); | ||
} | ||
|
||
fn main() { | ||
let a = A { field: 6u64 }; | ||
let num = 10u32; | ||
get_limits(a); | ||
get_limits(num); | ||
} | ||
|
||
``` | ||
|
||
Here we have a generalized function `get_limits` which accepts its argument with requirement for trait `Limits` implementation. As long | ||
as a type implements this trait, this function will succeed and will produce expected results. It's worth mentioning that a type can implement | ||
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.
Do you mean to say "this function will type check" or "this function will successfully compile" ? 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 function will successfully compile, thanks for this :) |
||
different limits type, not only for itself: `struct A` can have both `Limits<A>` and `Limits<u32>` implementations: we may simply add another | ||
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. What is the use case for 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.
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. On 2. I'm afraid not =) 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. So as I also don't remember why I did it so (what I was thinking about during writing), we may turn it into a simple trait then. |
||
implementation and use it in our generalized function appropriately: | ||
|
||
```rust | ||
impl Limits<u32> for A { | ||
fn min_value() -> u32 { | ||
0u32 | ||
} | ||
|
||
fn max_value() -> u32 { | ||
5u32 | ||
} | ||
} | ||
|
||
|
||
/// Will be called only if a type implements Limits with u32 value type. | ||
fn get_limits<T: Limits<u32> + std::fmt::Debug>(_t: T) { | ||
println!("Minimum value: {:?}", T::min_value()); | ||
println!("Maximum value: {:?}", T::max_value()); | ||
} | ||
``` | ||
|
||
Another option is to use the [`Bounded`](http://rust-num.github.io/num/num/trait.Bounded.html) trait of `num` crate: | ||
|
||
```rust | ||
trait Limits { | ||
fn min_value() -> Self; | ||
fn max_value() -> Self; | ||
} | ||
``` | ||
|
||
This will reduce the ambiguity of types and in most cases will be enough too. | ||
|
||
# How We Teach This | ||
I think the `Limits` name of a trait is appropriate: | ||
- `numeric_limits` is incorrect since the trait may be implemented for any type and it may be not numerical. | ||
- `Bounds` does not seem to be appropriate (personally for me). | ||
|
||
This feature does not involve anything into the language itself, but adds a trait into the standard library. All the primitive types | ||
and anything else what has `min_value()` and `max_value()` methods must implement this trait. Removing the type method is not required | ||
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. typo: what has => that has. 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.
Did you mean: Removing the inherent methods on the types is not required ? impl MyType {
fn inherent_method(self, arguments) -> return_type {..}
} Removing those methods would not only not be required but would also break backwards compatibility if done. 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 meant removing |
||
(why does it work with having both a trait implementation and a type method - I don't know). | ||
|
||
This feature can be introduced as `Limits` trait for the generalized contexts. | ||
|
||
# Drawbacks | ||
I don't know why we should not do this. | ||
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. A simple reason: This is possible as a crate. The RFC should show why this needs to be in the standard library. 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. Thanks, going to think how to write it there. |
||
|
||
# Alternatives | ||
Another option is to simply add macros for this: | ||
|
||
```rust | ||
macro_rules! max_value { | ||
($type: ident) => { | ||
$type::max_value() | ||
} | ||
} | ||
|
||
macro_rules! min_value { | ||
($type: ident) => { | ||
$type::min_value() | ||
} | ||
} | ||
``` | ||
|
||
This helps in generalizing the code too, but not in a way that the trait does. | ||
|
||
# Unresolved questions | ||
The trait design is arguable and is ready to accept any critic, namely what is better: generic trait or a simple one. | ||
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. If you can show why the "generic version" is needed and what use cases are enabled by it, then we are closer to solving this question. |
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.
This feels a bit thin, expanding this section a bit would be helpful.
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.
Okay. :) I will expand it.