On 06/09/2013 03:39 AM, Rémi Fontan wrote:
Hi,

I'm learning Rust and find it very interesting. My background is mostly python and C++. I'm now trying to get my head round the Rust generic system and was wondering whether there' s something equivalent to typedef in Rust?

my use case is:
- I have a generic 2d vector struct : struct Vec2x<T> {x:T, y:T}
- I would like to declare somethere in my code how to specialise this struct. I'm not sure yet whether it's going to be float or f64. So to avoid having to update float to f64 everywhere in the code, I was hoping I could write that:
    type Vec2f = Vec2x<float>
and then use Vec2f without worrying whether it's float or f64.

it seems to compile compiles, but the following did not work:
    let v = Vec2df:new(1.0, 2.0);

Rémi,

I ran into the same problem a while back and I fixed it using macros. If you're interested in my approach (which is not the most Rustic, but it's sufficient until we get type aliases that expose static functions), feel free to checkout

https://github.com/Jeaye/q3/blob/master/src/math/vec3.rs

The basic premise is to put all of the vec3 code into a macro, and then, similar to C++ templates, just plug in a type (at the bottom of the file when I specify the types of vec3s I want). The drawback to this is knowing the types ahead of time that you'd like, but this isn't too much of a problem for me. Maybe you can branch from this and find a solution that best fits you.

Cheers,
Jeaye
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to