Arrays and variadic parameters


(Alexandre Lopoukhine) #1

Hello all,

I'm not sure if this has been brought up before, or what the status of the issue is as of Swift 3, but I was wondering why there is no option to call a function that takes a variadic parameter argument with an array. Seen as the parameter essentially becomes an array, and is used as such in the function, I think that adding the ability to call it with one directly could be useful, with few downsides. This will, for example, allow you to use the built-in `min` and `max` functions on arrays.

WDYT?

– Sasha


(Charlie Monroe) #2

This has been brought up and there was a proposal by Haravikk, but the discussion started almost mid-July and the deadline for all Swift 3 *implementation* was on Jul 27. So it didn't make it, but I think there will be enough discussion about it on Aug 1 and after (all discussion about features for future Swift releases is currently on hold until Aug 1).

Unfortunately, the suggested behavior has an issue what to do when the array is empty. For example, you've mentioned min and max - there are (as variadic) ensured to always be called with at least one value. When you use it with an array, there is no guarantee that the array contains any element. In such cases, what should the min/max do? Crash? Return +-infinity?

Changing min/max to return an optional would be a solution, but I don't think a good one.

Moreover, for this particular example, you already have min/max on arrays:

let numbers = [23, 43, 12, 10]
numbers.minElement()

···

On Jul 29, 2016, at 11:46 AM, Alexandre Lopoukhine via swift-evolution <swift-evolution@swift.org> wrote:

Hello all,

I'm not sure if this has been brought up before, or what the status of the issue is as of Swift 3, but I was wondering why there is no option to call a function that takes a variadic parameter argument with an array. Seen as the parameter essentially becomes an array, and is used as such in the function, I think that adding the ability to call it with one directly could be useful, with few downsides. This will, for example, allow you to use the built-in `min` and `max` functions on arrays.

WDYT?

– Sasha
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Tim Vermeulen) #3

I agree mostly with what you’re saying, but variadic parameters don’t actually require at least one element. `max` simply returns a non-optional because its function signature has three non-variadic parameters as well:

public func max<T : Comparable>(_ x: T, _ y: T, _ z: T, _ rest: T...) -> T

···

This has been brought up and there was a proposal by Haravikk, but the discussion started almost mid-July and the deadline for all Swift 3 *implementation* was on Jul 27. So it didn't make it, but I think there will be enough discussion about it on Aug 1 and after (all discussion about features for future Swift releases is currently on hold until Aug 1).

Unfortunately, the suggested behavior has an issue what to do when the array is empty. For example, you've mentioned min and max - there are (as variadic) ensured to always be called with at least one value. When you use it with an array, there is no guarantee that the array contains any element. In such cases, what should the min/max do? Crash? Return +-infinity?

Changing min/max to return an optional would be a solution, but I don't think a good one.

Moreover, for this particular example, you already have min/max on arrays:

let numbers = [23, 43, 12, 10]
numbers.minElement()

> On Jul 29, 2016, at 11:46 AM, Alexandre Lopoukhine via swift-evolution<swift-evolution@swift.org>wrote:
>
> Hello all,
>
> I'm not sure if this has been brought up before, or what the status of the issue is as of Swift 3, but I was wondering why there is no option to call a function that takes a variadic parameter argument with an array. Seen as the parameter essentially becomes an array, and is used as such in the function, I think that adding the ability to call it with one directly could be useful, with few downsides. This will, for example, allow you to use the built-in `min` and `max` functions on arrays.
>
> WDYT?
>
> – Sasha
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution