Aliasing Things Other Than Types

Quick idea! What if we had a construct similar to typealias for aliasing things other than types such as properties, methods and free functions? Some examples:

extension Collection {
  // Aliasing an instance property.
  alias var size = count
}

extension Sequence {
  // Aliasing an instance method.
  alias func all(match:) = allSatisfy(_:)
  alias func any(match:) = contains(where:)
}

// Aliasing a free function.
alias func convolution(_:_:) = zip(_:_:)

// Aliasing a type.
alias protocol Sequence = Collection

// Aliasing a tuple.
alias Point = (x: Double, y: Double)

It might reduce the amount of bikeshedding seen in topics in Evolution as an alias is just a single line away. It would also be an extremely cool feature to have and beats writing wrappers.

Any thoughts?

I tend to be careful with aliasing as what’s won on the readability side is often lost on the simplicity side. So I think bikeshedding, while sometimes frustrating, is good after all, as it makes us agree on a single solution. I think the aliasing escape hatch would be too tempting :–)

6 Likes

These aliases are already very easy to write by just forwarding to the underlying property or method. I would say that the reason that there's not several different ways to spell things in the Swift standard library is unrelated to the ease of defining aliases, and is more about consistency/uniformity, etc.

2 Likes