More to the point, though, is 'sin' and 'cos' are also methods, so having 'to_degrees' as a method seems reasonable.
let r = f32::to_radians(5.0);
let x: f32 = 5.0;
let r = x.to_radians(); trait AngleConversion {
fn to_degrees(&self) -> Self;
fn to_radians(&self) -> Self;
}
impl AngleConversion for f32 {
...
}
impl AngleConversion for f64 {
...
}
Operators like + and / already live in traits; why not put methods like these into traits as well, so that users can implement them for their own types (variable-precision floats, decimal floating point, etc)?(As a side note, can a trait accept an AsRef<Self> in place of &self or self, so that register-sized objects implementing Copy don't need to use &self?)
Plus, since you can define your own traits, if this is functionality you need, you can just do it, and delegate to the inherent implementation for those two types.
I agree with the caution though. This takes time to get right.
value.to_radians().to_radians()
then? Weird.That said, this is probably such a simple use that it's not worth having Degree and Radian classes in core.
This sort of stuff works really well when the type system incorporates units of measure directly (usually as some kind of generic type parameter). F# is a good example.
var s1 = "blah"
var s2 = s1 as string
var s3 = s2 as string
edit: Nevermind, I guess you're wanting like types for units of measurement. Sounds interesting.Is the best way to solve that problem by introducing "unit measurement" types that are more specific than general numerical types? Or is there another way? Or even a general concept I can go read about? Thanks in advance!