Skip to content

Instantly share code, notes, and snippets.

@loiclec
Last active December 6, 2015 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loiclec/22459d230a21dbcb81fc to your computer and use it in GitHub Desktop.
Save loiclec/22459d230a21dbcb81fc to your computer and use it in GitHub Desktop.
Swift: Formal proposal to introduce new specific keyword for associated type declarations.

Replace typealias keyword with associated for associated type declarations

Introduction

The typealias keyword is currently used to declare two kinds of types:

  1. Type Aliases (alternative name for an existing type)
  2. Associated Types (placeholder name to type used as part of a protocol)

These two kinds of declarations are different and should use distinct keywords. This would emphasize the difference between them and reduce some of the confusion surrounding the use of associated types.

The proposed new keyword is associated.

Motivation

The recycling of typealias for associated type declarations leads to confusion in many ways.

  1. It is not obvious that typealias in protocols means something else than in other places.
  2. It hides the existence of associated types to beginners, which allows them to write code they misunderstand.
  3. It hides the absence of concrete type aliases inside protocols.

In particular, 2 + 3 leads to programmers writing

protocol Prot {
    typealias Container : SequenceType
    typealias Element = Container.Generator.Element
}

without realizing that Element is a new associated type with a default value of Container.Generator.Element instead of a type alias to Container.Generator.Element.

However, this code

protocol Prot {
    typealias Container : SequenceType
}
extension Prot {
    typealias Element = Container.Generator.Element
}

declares Element as a type alias to Container.Generator.Element.

These subtleties of the language currently require careful consideration to understand.

Proposed solution

Replace the typealias keyword with associated.

This solves every issue mentioned above:

  1. typealias can only be used for type aliases declaration.
  2. Beginners are forced to learn about associated types when creating protocols.
  3. An error message can be displayed when someone tries to create a type alias inside a protocol.

This eliminates the confusion showed in the previous code snippets.

protocol Prot {
    associated Container : SequenceType
    typealias Element = Container.Generator.Element // error: cannot declare type alias inside protocol, use protocol extension instead
}
protocol Prot {
    associated Container : SequenceType
}
extension Prot {
    typealias Element = Container.Generator.Element
}

Alternative keywords considered: withtype, associatedtype, typeassociation, type

Proposed Approach

I suggest introducing associated and deprecating typealias in Swift 2.2, and removing typealias entirely in Swift 3.

Impact on existing code

As it simply replaces one keyword for another, the transition to associated could be easily automated without any risk of breaking existing code.

@alex-lew
Copy link

alex-lew commented Dec 6, 2015

When you say "deprecate typealias entirely in Swift 3," do you mean "as a synonym for associated," or "as a language feature"? Presumably you do want to support normal type aliases.

Another possible addition to this proposal: with the new associated keyword, don't allow default values being set in a protocol.

protocol Proc {
    associated Container: SequenceType
    associated Element = Container.Generator.Element // should not be allowed
}

It seems weird that you can't declare any other "default values" inside a protocol, but you can for associated types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment