Named parameters


(David Keck) #1

Beginner here, so this may be obvious to some people, but I can't figure out why the first argument in a function call isn't referenced by name, but all subsequent ones are. I think I have the syntax down, but I just don't get why it is that way.

For example:
func add(notRequired: Int, required: Int) -> Int {
  return notRequired + required
}

let result = add(2, required: 5)
// Not allowed:
// let result = add(notRequired: 2, required: 5)

I just want to know the rationale behind this.


(Austin Zheng) #2

Hi David,

The given rationale is that the first argument is often the "direct object" to a subject-verb phrase formed by a method call, and hence deserves privileged treatment. There's actually a thread going on in the swift-evolution mailing list about whether this should be changed.

Best,
Austin

···

On Jan 23, 2016, at 12:02 PM, David Keck via swift-users <swift-users@swift.org> wrote:

Beginner here, so this may be obvious to some people, but I can't figure out why the first argument in a function call isn't referenced by name, but all subsequent ones are. I think I have the syntax down, but I just don't get why it is that way.

For example:
func add(notRequired: Int, required: Int) -> Int {
  return notRequired + required
}

let result = add(2, required: 5)
// Not allowed:
// let result = add(notRequired: 2, required: 5)

I just want to know the rationale behind this.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


#3

It has origin in Obj C:
excelent explaining here:
http://stackoverflow.com/questions/24680771/using-function-parameter-names-in-swift

Dne 23.1.2016 v 21:02 David Keck via swift-users napsal(a):

···

Beginner here, so this may be obvious to some people, but I can't figure out why the first argument in a function call isn't referenced by name, but all subsequent ones are. I think I have the syntax down, but I just don't get why it is that way.

For example:
func add(notRequired: Int, required: Int) -> Int {
  return notRequired + required
}

let result = add(2, required: 5)
// Not allowed:
// let result = add(notRequired: 2, required: 5)

I just want to know the rationale behind this.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users