Allow a typealias to be named after the corresponding type


(Tim Vermeulen) #1

public struct BinarySearchTree<Key: Comparable, Value> {
    typealias Node = Node<Key, Value>
    var root: Node?
}

private class Node<Key: Comparable, Value> { }

This code currently doesn’t compile because the Node typealias circularly references itself. I’d like this syntax to just work, because this are the alternatives:
- rename the typealias to something like `TheNode`
- rename the Node class
- don’t use a typealias at all

None of them are very favorable. Allowing this proposed syntax will (in my opinion) lead to more readable code. In fact, inside the BinarySearchTree struct, BinarySearchTree is already an implicit typealias of BinarySearchTree<Key, Value>.

The only problem I foresee is that a generic typealias could cause confusion, so perhaps that shouldn’t be allowed here.


(Brent Royal-Gordon) #2

public struct BinarySearchTree<Key: Comparable, Value> {
    typealias Node = Node<Key, Value>
    var root: Node?
}

private class Node<Key: Comparable, Value> { }

This code currently doesn’t compile because the Node typealias circularly references itself. I’d like this syntax to just work, because this are the alternatives:
- rename the typealias to something like `TheNode`
- rename the Node class
- don’t use a typealias at all

- fully qualify the class name with the name of the module:

  typealias Node = DataStructureKit.Node<Key, Value>

···

--
Brent Royal-Gordon
Architechies


(Haravikk) #3

This. Though I do sympathise with the issue, as it’s kind of strange that a circular reference is assumed rather than searching for something else in scope, as this differs from how things like variable shadowing behave.

That said, I think fully qualifying the type is the right solution in this case, but I wonder if we might consider a fixit capable of suggesting possible matching types?

···

On 15 Jun 2016, at 09:35, Brent Royal-Gordon via swift-evolution <swift-evolution@swift.org> wrote:

public struct BinarySearchTree<Key: Comparable, Value> {
   typealias Node = Node<Key, Value>
   var root: Node?
}

private class Node<Key: Comparable, Value> { }

This code currently doesn’t compile because the Node typealias circularly references itself. I’d like this syntax to just work, because this are the alternatives:
- rename the typealias to something like `TheNode`
- rename the Node class
- don’t use a typealias at all

- fully qualify the class name with the name of the module:

  typealias Node = DataStructureKit.Node<Key, Value>


(Tim Vermeulen) #4

This doesn’t work if `DataStructureKit` and `BinarySearchTree` have the same name.

···

> public struct BinarySearchTree<Key: Comparable, Value>{
> typealias Node = Node<Key, Value>
> var root: Node?
> }
>
> private class Node<Key: Comparable, Value>{ }
>
> This code currently doesn’t compile because the Node typealias circularly references itself. I’d like this syntax to just work, because this are the alternatives:
> - rename the typealias to something like `TheNode`
> - rename the Node class
> - don’t use a typealias at all
- fully qualify the class name with the name of the module:

typealias Node = DataStructureKit.Node<Key, Value>

--
Brent Royal-Gordon
Architechies