ios - GeneratorOf<T> Cannot find an initializer Error -


my attempts understand generators , sequences lead me idea implement fibonacci sequence. works perfect:

struct fibonaccigenerator : generatortype {     typealias element = int      var current = 0, nextvalue = 1      mutating func next() -> int?     {         let ret = current         current = nextvalue         nextvalue = nextvalue + ret         return ret     } }   struct fibonaccisequence : sequencetype {     typealias generator = fibonaccigenerator      func generate() -> generator     {         return fibonaccigenerator()     } } 

then decide use sequenceof , generatorof same, stuck on generatorof gives me error "cannot find initializer type 'generatorof' accepts argument list of type '(() -> _)'" next code.

var current = 0 var nextvalue = 1  var fgof = generatorof{     let ret = current         current = nextvalue         nextvalue = nextvalue + ret         return ret } 

enter image description here

but if wrap function works fine:

    func getfibonaccigenerator() -> generatorof<int> {      var current = 0     var nextvalue = 1      return generatorof{         let ret = current         current = nextvalue         nextvalue = nextvalue + ret         return ret     } } 

why works differently? xcode bug or miss something?

the closure used in initializer of generatorof has type () -> t?, , in

var fgof = generatorof {     let ret = current     current = nextvalue     nextvalue = nextvalue + ret     return ret } 

the compiler cannot infer t is. can either make block signature explicit

var fgof = generatorof { () -> int? in     let ret = current     current = nextvalue     nextvalue = nextvalue + ret     return ret } 

or specify type of generator with

var fgof = generatorof<int> {      let ret = current     current = nextvalue     nextvalue = nextvalue + ret     return ret } 

in last example

func getfibonaccigenerator() -> generatorof<int> {     // ...     return generatorof {         // ...         return ret     } } 

it works because type inferred context (i.e. return type of getfibonaccigenerator()).


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -