ngn999 ngn999 - 4 years ago 143
Swift Question

Chain style expression error on swift generator

swift repl:

repl.swift:1:22: error: value of type 'Zip2Generator<IndexingGenerator<Array<Int>>, IndexingGenerator<Array<Int>>>' has no member 'next'

but this works:

var gen = zip([1,2,3],[7,8,9]).generate()


Answer Source

The function next() mutates the object that it is called on, so that object must be assigned to a var for it to work. zip([1,2,3],[7,8,9]).generate() is immutable until you assign it to a var.

Why does next() mutate the object? Because there is an internal index variable that is keeping track of which value to show next. next() uses that internal index and then advances it (mutates it) to point to the next item.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download