struct A ();
impl A {
fn get(&self) -> [f64; 1] { [0.] }
}
fn main() {
for _ in A().get() {}
}
compiles fine but the slight modification (playground)
struct A {}
impl A {
fn get(&self) -> [f64; 1] { [0.] }
}
fn main() {
for _ in A{}.get() {}
}
doesn't (I have to write (A{}.get()) for it to work). I'm a bit surprised by this discrepancy. Is it simply a limitation of the parser or is there a more fundamental reason? (Of course, I could make get a pure function but, in my real case, it comes from A implementing some trait...)
For the empty A {}, you could make your own const A but... probably I'd say just #[derive(Default)] and call A::default() instead.
Now let's look at why the tuple struct works...
With the first, struct A() also creates a constructor function (or function-like [1]), fn A() -> A, and you're [2] calling that function. It chains together like you're use to functions doing.