function having methods
About
While going through the Learning Go - An Idiomatic Approach to Real-World Go Programming book by Jon Bodner, I learned that functions can have methods.
package main
import "fmt"
type Foobar func()
func (foobar Foobar) Run() {
foobar()
}
func main() {
helloworld := func() {
fmt.Println("helloworld")
}
foobar := Foobar(helloworld)
foobar.Run()
}This adds to the "first-class functions" principle that Go adheres to.
Rust does it too?!
Not too long after, I was also surprised to discover a similar pattern in a Rust library:
trait Foobar {
fn run(&self);
}
impl<F> Foobar for F
where
F: Fn(),
{
fn run(&self) {
self()
}
}
fn main() {
let it = || println!("Hello, world!");
it.run();
}It's only a little bit different as we don't need a type conversion; we can invoke the run method differently on the closure!