Ali
Ali

Reputation: 515

What is the point of having two different names for the same parameter?

func mapEachElement (inArray arr: [Int],  withFunc aFunc: (Int))

Why would there be "inArray and then "arr"...what's the point?

Same for "withFunc and "aFunc", it makes it more code complicated and also messier to read, why are they even used?

Upvotes: 13

Views: 741

Answers (3)

Duncan C
Duncan C

Reputation: 131426

I'm not sold on the issue of having different internal and external names either.

Having or not having external names makes sense to me.

object.BeepXTimes(6) doesn't need an external name for it's parameter because the function name implies the meaning of the parameter.

The rationale, I think, is that sometimes the naming of a function's external parameters is phrased to make sense to the caller in a sentence:

someView.animateInWithDuration(0.25, withTimingFunction: .easeIn)

The external name withTimingFunction makes the function call read like a sentence, but withTimingFunction makes a lousy internal parameter name. You would probably want your parameter name to be just timingFunction. Having separate internal and external parameter names lets you do that.

For me, though, the extra complexity this requires in the function definition doesn't seem worth it. The good news is that the default is to create parameters using the same name for both internal and external parameter names, so the standard syntax is simple and easy to read.

Upvotes: 7

jtbandes
jtbandes

Reputation: 118691

These are internal and external parameter names.

Just like in Obj-C, you might have

- (void)calculateFoo:(id)foo withBar:(id)bar ...

[object calculateFoo:var1 withBar:var2];

in Swift, we use

func calculateFoo(foo: AnyObject, withBar bar: AnyObject) ...

object.calculateFoo(var1, withBar: var2)

The internal names foo and bar are only accessible inside the function/method. The external names provide the argument labels which you use to call the function.

In your case, you need to use the internal name nibBundle. That's the one accessible inside the function body.

Upvotes: 0

neo
neo

Reputation: 2042

inArray is external name which the caller of the function should use when passing parameters. arr is the internal name which the function implementer uses in the implementation to refer to the parameter. You don't have to supply external name.It makes it more readable. It is more like to make swift function names and parameters readable as Objective-C functions are.

Upvotes: 13

Related Questions