Instance Methods Are Curried Functions

One cool thing to note is that instance methods are actually curried functions in Swift.

The basic idea behind currying is that a function can be partially applied, meaning that some of its parameter values can be specified (bound) before the function is called. Partial function application yields a new function.

So given that I have a class:

 class MyHelloWorldClass {
 
  func helloWithName(name: String) -> String {
  return "hello, \(name)"
  }
 }

I can create a variable that points to the class’s helloWithName function:

 let helloWithNameFunc = MyHelloWorldClass.helloWithName
 // MyHelloWorldClass -> (String) -> String

My new helloWithNameFunc is of type MyHelloWorldClass -> (String) -> String, a function that takes in an instance of my class and returns another function that takes in a string value and returns a string value.

So I can actually call my function like this:

 let myHelloWorldClassInstance = MyHelloWorldClass()
 
 helloWithNameFunc(myHelloWorldClassInstance)("Mr. Roboto")
 // hello, Mr. Roboto