2

To gain a better understanding of Swift, I've extended SequenceType to add my own versions of map, reduce and forEach functions. However, the filter function is different in that it returns an array of Self.Generator.Element. Here's what I have so far:

extension SequenceType {
    public func myFilter(@noescape includeElement: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.Generator.Element] {
        var array = [Self.Generator.Element]()
        for element in self {
            do {
                if try includeElement (element) {
                    array.append(element)
                }
            }
        }
        return array
    }
}

The statement var array = [Self.Generator.Element]() produces an "Invalid use of () to call a value of non-function type [Self.Generator.Element.Type]" error. My question is how do I create/add to/return an array of Self.Generator.Element?

1
  • Excellent question, and I've have upvoted it two if I could! The two respondents beat me to the answer, and @RobNapier's explanation is very clear. Given that you are investigating the "hows" of Swift, maybe you could expand your question with some comments on the more obscure aspects of the implementation, like why we have to use @noescape... Commented Feb 23, 2016 at 18:30

2 Answers 2

5

Not sure why, but Xcode is fine with following syntax:

extension SequenceType {
    public func myFilter(@noescape includeElement: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.Generator.Element] {
        var array : [Self.Generator.Element] = []
        for element in self {
            do {
                if try includeElement (element) {
                    array.append(element)
                }
            }
        }
        return array
    }
}

The only change is the array declaration (var array : [Self.Generator.Element] = []).

Sign up to request clarification or add additional context in comments.

Comments

4

Swift's type inference is getting confused about what you mean. This could mean two things:

var array = [Self.Generator.Element]()

You might mean (and do mean) that you want to call the constructor for the type Array<Self.Generator.Element>.

But you might mean (and the compiler thinks you mean) that you want to create an array of Self.Generator.Element.Type and then you want to call it as a function.

I don't remember this being a compiler confusion in the past, and it may be a regression. That said, it is ambiguous (though only one really makes sense in the end, and I would think you'd need to have said Generator.Element.self there to refer to the type, so maybe it's a real compiler bug). You can remove the ambiguity by using the preferred way to initialize arrays, by using their type:

var array: [Self.Generator.Element] = []

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.