Why can't I use a short form for the parameter's type?

This is in an auxiliary file for a Swift playground.

extension LimitedPrimalityViaTrialDivisionNodesSequence.Iterator.Node {
    /// Creates a clone of the given node, along with clones of its followers.
    convenience init(cloning source: LimitedPrimalityViaTrialDivisionNodesSequence.Iterator.Node) {
        self.init(source.value)
        next = source.next.map { Self(cloning: $0) }
    }
}

I originally had "Node" as the parameter type and in the closure in the last line of code. The compiler didn't accept them. I changed them to "Self," but it only accepted its use within the initializer. I had to use the fully qualified form in the initializer's argument. Why? I should be able to use the class's name as a parameter type. (I tried "Iterator.Node," but that didn't work either.)

If it needs to be picky over derived classes, we could use Self to always mean the exact class and the class's name for that class or any derived types.

In my code, Node is a final class, if that makes a difference.

It doesn't work here either:

// Appends the given value to the end of this node's chain.
func append(_ value: Integer) {
    unowned var node: Node = self
    while let nextNode = node.next {
        node = nextNode
    }
    node.next = Self(value)
}

Nor can I use "Self" in the declaration. That's just {INSERT EXPLETIVE} wrong.

Are these supposed to work, and my compiler is fried right now? Or do I have to file a bug report for these deficiencies?

I don’t think Node will work.AFAIU, it would

  1. Search LimitedPrimalityViaTrialDivisionNodesSequence.Iterator.Node namespace, resulting in LimitedPrimalityViaTrialDivisionNodesSequence.Iterator.Node.Node
  2. Search global namespace, resulting in Node

Neither of which is the type you want.
I’m not sure about Self though.

Terms of Service

Privacy Policy

Cookie Policy