Re: Swift 2.0 difficulty
Re: Swift 2.0 difficulty
- Subject: Re: Swift 2.0 difficulty
- From: Roland King <email@hidden>
- Date: Mon, 29 Jun 2015 15:31:08 +0800
> On 29 Jun 2015, at 14:37, Michael de Haan <email@hidden> wrote:
>
>
> Hi All
> I am looking at Swift 2.0. The compiler has converted the code below to the “latest” Swift syntax. This causes the error shown. I have spent a few days trying to get this to compile. What is confusing to me, is that the code, listed last (func removeAnElemen…..) does compile ( all in Playground) and there 'anyGenerator' seems to tolerate some logic in the body of “anyGenerator”.
> Any insight will be much appreciated.
>
>
>
> // This is OLD Swift
> // Generators and sequences
> var currentValue = 1
> let newGenerator = GeneratorOf<Int> {
> let previousValue = currentValue
> currentValue = currentValue * 2
> if (previousValue > 20) { return nil }
> return previousValue
> }
> let generatedArray = Array(newGenerator)
>
>
>
>
> // This is Swift 2.0
> // Generators and sequences
> var currentValue = 1
> let newGenerator = anyGenerator { // Error: Cannot find an overload for ‘anyGenerator’ that accepts an argument list of type ‘(()-> _)
> let previousValue = currentValue
> currentValue = currentValue * 2
> if (previousValue > 20) { return nil }
> return previousValue
> }
> let generatedArray = Array(newGenerator)
>
>
> This does compile:
>
> func removeAnElement<T>(array: [T]) -> AnyGenerator<[T]> {
> var i = 0
> return anyGenerator {
> if i < array.count {
> var result = array.self
> result.removeAtIndex(i)
> i++
> return result
> }
> return nil
> }
> }
> Array(removeAnElement([1,2,3]))
> _______________________________________________
>
var currentValue = 1
let newNewGenerator = anyGenerator
{
()->Int? in
let previousValue = currentValue
currentValue *= 2
return ( previousValue > 20 ) ? nil : previousValue
}
let newGeneratedArray = Array( newNewGenerator )
For some reason you have to specify block signature, doesn’t seem to be able to work it out despite the example showing it should and your second example showing it does. The ‘_’ in the ‘()->_’ is usually a hint it’s a bit confused.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden