Want to head some comments about the thought: why self.method
couldn't be expression e.g. be equivalent |v| self.method(v)
. For now, methods are distinct from field and cannot be accessed as some value or function pointer. However, it is mostly similar to statefull closure, capturing self
, and usage of FnOnce
, FnMut
or Fn
can be deduced from method's receiver type.
A big problem is that fields and methods have separate namespaces, so the following is possible and indeed somewhat common:
struct Foo { bar: i32 }
impl Foo {
fn bar(&self) -> i32 { self.bar }
}
Currently foo.bar
is unambiguous, but wouldn't be if it could also refer to the method of the same name.
5 Likes
Even more ambiguous:
struct Bar {
foo: fn(i32) -> i32,
}
impl Bar {
fn foo(x: i32) -> i32 { ... }
}
5 Likes
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.