-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add value type parameters RFC #174
Conversation
Thanks for writing this up! This is definitely a desirable feature but should be able to be added after 1.0 in a backward compatible way. Also it seems quite similar to #56, could read over that and highlight any differences? (If there aren't any meaningful ones, we can close this with the |
This RFC is incredibly light on details, and offers no concrete analysis on the impact to the type system or how this would be implemented, only presenting a "feel" for what this feature should look + act like. Specific feedback coming later, short on time atm. |
); | ||
) | ||
|
||
fn print_int<num>() { |
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 still has to be const num: int
Allowing I discussed this already with @Zoxc in IRC, but the short overview is as follows: A trait can declare associated values via the pub trait Numeric {
static MAX_VALUE: Self;
static MIN_VALUE: Self;
...
} Similarly, impl Numeric for uint {
static MAX_VALUE: uint = -1u;
static MIN_VALUE: uint = 0u;
...
} With this ability, you can now provide the desired generic parameterized values by instead using a generic type that's bounded on a trait with the necessary values. This approach should work for all generic value parameters. There's still a desire for This would also generalize into an associated type scheme where Combined with field offsets (RFC PR #175), this would allow you to basically define a trait that requires implementors to provide certain fields. Clients of the trait can then access the fields via the associated values, and as long as the trait Offset<T,Ty> {
static OFFSET: FieldOffset<T,Ty>;
}
struct Object {
x: uint,
y: uint
}
impl Object {
/// Phantom nested type that conforms to Offset for field `x`
struct FieldX;
/// Phantom nested type that conforms to Offset for field `y`
struct FieldY;
// this impl could alternatively go one level up and use `for Object::FieldX`
// but I think it makes a bit more sense to be nested too
impl Offset<Object,uint> for FieldX {
static OFFSET: FieldOffset<Object,uint> = offsetof Object.x;
}
impl Offset<Object,uint> for FieldY {
static OFFSET: FieldOffset<Object,uint> = offsetof Object.y;
}
} With the above, you can now write code that looks like fn foo<T, Field: Offset<T,uint>>(x: &mut T) {
// double the given field
*Field::OFFSET.get_mut(x) *= 2;
...
}
fn bar(x: &mut Object) {
foo::<Object, Object::FieldX>(x);
} The above is kind of trivial, but it actually represents something you can't do today, which is to provide mutable access to an object and to a field within the object simultaneously. Normally to provide field access you'd pass a A while back Servo wanted to be able to have traits define fields that implementors must provide. This was so the DOM implementation could directly access various common fields across DOM nodes without needing struct inheritance. Assuming it optimizes as well as I hope it will, I think associated values + field offsets would actually solve this need. |
@kballard The entire point of this is to avoid having a type per value, although grouping often used multiple values together in a single type with associated items can be an alternative in certain cases. Furthermore your example doesn't use OFFSET in an static, just in a regular expressions, so it doesn't even need associated items. There doesn't seem to be any significant differences with #56, so you can postpone this too. |
@Zoxc The use of |
This feature comes up often. It's desirable, and it will probably happen, but not before 1.0. Let's postpone this and come back to it in the future. |
Rendered view