DEV Community

Discussion on: Go's method is curried funtion

Collapse
 
nekketsuuu profile image
Takuma Ishikawa

Interesting! However, what do you mean by the words "curried function" in the title? I think Foo.doSomething is not curried. For example, if doSomething takes one argument, Foo.doSomething(123) causes an error.

func (f Foo) doSomething(b bool) {
    fmt.Println(f, b)
}

func main() {
    Foo.doSomething(123)  // error: not enough arguments
}

Maybe you want to say that Go can pass a receiver to a method value as an argument?

Collapse
 
mattn profile image
Yasuhiro Matsumoto • Edited

Yes, in strictly, as you think, this is not currying. The currrying is a transform taking one less argument.

package main

import (
    "fmt"
)

func add(n int) func(int) int {
    return func(v int) int {
        return n + v
    }
}

func main() {
    add10 := add(10)
    fmt.Println(add10(5))
}

Just metaphor :)

If this function object for currying, you can try this.

package main

import (
    "fmt"
)

type number int

func (f number) add(b number) number {
    return f + b
}

func main() {
    fmt.Println(number(3).add(4))
    fmt.Println(number.add(3, 4))
}