Hi everyone!
I’m migrating some code from Swift 2.2. to Swift 3 and something weird happened.
Is this is a bug or I’m missing something?
// Swift 2.2
func quickSort(_ array: [Int]) -> [Int] {
guard array.count > 1 else {
return array
}
let (pivot, rest) = (array.first!, array.dropFirst())
let lessThan = rest.filter({ $0 < pivot })
let greaterThanOrEqual = rest.filter({ $0 >= pivot })
// Can use ‘[pivot]' here, no problem at all
return quickSort(lessThan) + [pivot] + quickSort(greaterThanOrEqual)
}
// Swift 3
func quickSort(_ array: [Int]) -> [Int] {
guard array.count > 1 else {
return array
}
let (pivot, rest) = (array.first!, array.dropFirst())
let lessThan = rest.filter({ $0 < pivot })
let greaterThanOrEqual = rest.filter({ $0 >= pivot })
// Cannot use ‘[Int]' here, as in Swift 2.2
// Error says 'Int' is not convertible to '[Int]'
let pivotArray = [pivot]
return quickSort(lessThan) + pivotArray + quickSort(greaterThanOrEqual)
}
Best,
— A
gribozavr
(Dmitri Gribenko)
2
Hi Adriano,
This code compiles with the current git master. Could you
double-check and post the exact error you are getting?
Dmitri
···
On Mon, Jun 27, 2016 at 7:48 AM, Adriano Ferreira via swift-users <swift-users@swift.org> wrote:
// Swift 3
func quickSort(_ array: [Int]) -> [Int] {
guard array.count > 1 else {
return array
}
let (pivot, rest) = (array.first!, array.dropFirst())
let lessThan = rest.filter({ $0 < pivot })
let greaterThanOrEqual = rest.filter({ $0 >= pivot })
// Cannot use ‘[Int]' here, as in Swift 2.2
// Error says 'Int' is not convertible to '[Int]'
let pivotArray = [pivot]
return quickSort(lessThan) + pivotArray + quickSort(greaterThanOrEqual)
}
--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/
Hi,
So, I tried :
// Swift 3
func quickSort(_ array: [Int]) -> [Int] {
guard array.count > 1 else {
return array
}
let (pivot, rest) = (array.first!, array.dropFirst())
let lessThan = rest.filter({ $0 < pivot })
let greaterThanOrEqual = rest.filter({ $0 >= pivot })
// If a separate the sorting into variables rather than
// calling them direct before 'return' it works
let lessThanSorted = quickSort(lessThan)
let greaterThanOrEqualSorted = quickSort(greaterThanOrEqual)
return lessThanSorted + [pivot] + greaterThanOrEqualSorted
}
Here the exact error I’m getting:
Thanks for helping.
— A
···
On Jun 27, 2016, at 12:38 PM, Dmitri Gribenko <gribozavr@gmail.com> wrote:
On Mon, Jun 27, 2016 at 7:48 AM, Adriano Ferreira via swift-users > <swift-users@swift.org> wrote:
// Swift 3
func quickSort(_ array: [Int]) -> [Int] {
guard array.count > 1 else {
return array
}
let (pivot, rest) = (array.first!, array.dropFirst())
let lessThan = rest.filter({ $0 < pivot })
let greaterThanOrEqual = rest.filter({ $0 >= pivot })
// Cannot use ‘[Int]' here, as in Swift 2.2
// Error says 'Int' is not convertible to '[Int]'
let pivotArray = [pivot]
return quickSort(lessThan) + pivotArray + quickSort(greaterThanOrEqual)
}
Hi Adriano,
This code compiles with the current git master. Could you
double-check and post the exact error you are getting?
Dmitri
--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/
gribozavr
(Dmitri Gribenko)
4
I see. I think this is a compiler bug, I filed
[SR-1914] Array '+' fails to type check · Issue #44523 · apple/swift · GitHub . Thanks!
Dmitri
···
On Mon, Jun 27, 2016 at 9:44 AM, Adriano Ferreira <adriano.ferreira@me.com> wrote:
Here the exact error I’m getting:
--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/
Thanks Dmitri.
If use typecasting it seems to work again. Take a look below:
// Swift 3
func quickSort(_ array: [Int]) -> [Int] {
guard array.count > 1 else {
return array
}
let (pivot, rest) = (array.first!, array.dropFirst())
let lessThan = rest.filter({ $0 < pivot })
let greaterThanOrEqual = rest.filter({ $0 >= pivot })
// If I typecast ‘[pivot]’ to ‘[Int]' it now works
return quickSort(lessThan) + ([pivot] as [Int]) + quickSort(greaterThanOrEqual)
}
Cheers,
— A
···
On Jun 27, 2016, at 1:01 PM, Dmitri Gribenko <gribozavr@gmail.com> wrote:
On Mon, Jun 27, 2016 at 9:44 AM, Adriano Ferreira > <adriano.ferreira@me.com> wrote:
Here the exact error I’m getting:
I see. I think this is a compiler bug, I filed
https://bugs.swift.org/browse/SR-1914 . Thanks!
Dmitri
--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/