Example: Say we want to make a function to test if two vectors of floats are approximately equal, up to some tolerance. The signature could be
fn almost_equal(vec_1: &Vec<f32>, vec_2: &Vec<f32>, rtol: f32, atol: f32) -> bool;
where rtol and atol are relative and absolute tolerances for equality. However, it’d be nice to have some sensible defaults for rtol and atol. The way I thought to accomplish this (would love to hear of a better way though!) is to instead have the signature be
fn almost_equal(vec_1: &Vec<f32>, vec_2: &Vec<f32>) -> AlmostEqualResult;
where AlmostEqualResult is (ignoring lifetime details etc.)
struct AlmostEqualResult {
vec_1: &Vec<f32>,
vec_2: &Vec<f32>,
rtol: f32,
atol: f32,
}
and contains a function
pub fn with_rtol(self, rtol: f32) -> Self {
Self {
rtol: rtol,
..self,
}
}
(and similarly for atol). Then it would be great to be able to implement e.g. Into<bool> for AlmostEqualResult (or perhaps a new Bool trait would be appropriate instead…) to allow things like:
if almost_equal(&vec_1, &vec_2).with_rtol(1e-3) {
blah blah
}
Right now the signature would require something like (IIUC)
if almost_equal(&vec_1, &vec_2).with_rtol(1e-3).bool() {
blah blah
}
which is more cumbersome/less readable IMO.