Hacker News new | ask | show | jobs
by ridiculous_fish 3319 days ago
Named parameters are very much the default in Swift. Positional parameters are rare exceptions.

The internal/external name divide often works out beautifully. For example, let's equip Double with a multiply-add. An idiomatic Swift signature:

    extension Double {
      func multiply(by multiplier:Double, adding summand:Double) -> Double {
        return self * multiplier + summand
      }
    }

    print(3.multiply(by:4, adding:5))
Erase punctuation and you have:

    func multiply by multiplier adding summand

    print 3 multiply by 4 adding 5
The caller sees an action (multiply by, adding), the callee sees nouns (multiplier, summand). It's fantastically readable.
1 comments

The javascript equivalent would be something like

    Number.prototype.multiply = function({by:multiplier, adding:summand = 0}) {
	return this * multiplier + summand
    }

    console.log((3).multiply({by: 4, adding: 5}))
But of course, javascript APIs are hardly ever written like that. I think it's interesting how the norms of your ecosystem and very small differences in ergonomics in expression make a big difference in behaviour.