Ashish Myles wrote:
So, I am to presume from your answer that there is no way to create
class-associated constants? Is this worth considering adding to Rust?
Rust does not, in fact, have classes. However, what I think you are
attempting to achieve is to have the ability to write generic code on
some type T that is either Vector2 or Vector3, and then one could write
T::len to reference the relevant constant. The way to do this in Rust
would probably be with a trait and a static fn:
trait VectorN {
static fn len() -> uint; // Eventually: just fn len() -> uint
}
fn foo<V: VectorN>(v: &V) { /* In theory, V::len() would give the
length */ }
Problem is, you can't write static functions that do not reference
`Self` today [1].
In any case what you *really* want is something like
trait VectorN {
const len: uint;
}
fn foo<V: VectorN>(v: &V) { /* Here, V::len is a constant */ }
which certainly doesn't work today.
There is some design work needed here. In general, this seems to be
related to the desire for associated types [2]. We need to spend some
time thinking about what we want to support. I imagine we'll wind up
postponing some or all such features to after Rust 1.0.
I guess the closest thing that does work today is something like:
trait VectorN {
fn len(&self) -> uint;
}
fn foo<V: VectorN>(v: &V) { /* v.len() gives the length */
This has the downside that it requires an instance of `V` to be used.
Niko
[1] I thought there was an issue on this, but I couldn't find it.
[2] https://github.com/mozilla/rust/issues/5033
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev