Uh oh!
There was an error while loading. Please reload this page.
Add value type parameters RFC - #174
Conversation
huonw
commented
Jul 21, 2014
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 |
emberian
commented
Jul 21, 2014
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. |
lilyball
commented
Jul 22, 2014
Allowing I discussed this already with @Zoxc in IRC, but the short overview is as follows: A trait can declare associated values via the pubtraitNumeric{staticMAX_VALUE:Self;staticMIN_VALUE:Self;
...
}Similarly, implNumericforuint{staticMAX_VALUE:uint = -1u;staticMIN_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 traitOffset<T,Ty>{staticOFFSET:FieldOffset<T,Ty>;}structObject{x:uint,y:uint}implObject{/// Phantom nested type that conforms to Offset for field `x`structFieldX;/// Phantom nested type that conforms to Offset for field `y`structFieldY;// 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 tooimplOffset<Object,uint>forFieldX{staticOFFSET:FieldOffset<Object,uint> = offsetof Object.x;}implOffset<Object,uint>forFieldY{staticOFFSET:FieldOffset<Object,uint> = offsetof Object.y;}}With the above, you can now write code that looks like fnfoo<T,Field:Offset<T,uint>>(x:&mutT){// double the given field*Field::OFFSET.get_mut(x) *= 2;
...}fnbar(x:&mutObject){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. |
Zoxc
commented
Jul 22, 2014
@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. |
lilyball
commented
Jul 22, 2014
@Zoxc The use of |
brson
commented
Aug 1, 2014
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