Uploaded image for project: 'Swift'
  1. Swift
  2. SR-14011

Guide existential uses of non-existential protocols toward generic parameterization

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:
      None

      Description

      It’s common for folks to reach for existentials in places we don’t allow them, e.g.,

      struct ContainerView: View {
          var childView: View   // error: Protocol ‘View’ can only be used as a generic constraint because it has Self or associated type requirements
        }
      

      In some common scenarios, such as stored properties of a struct, cases of an enum, or parameters of a function, the answer is to parameterize the enclosing entity, e.g.,

        struct ContainerView<V: View>: View {
          var childView: V
        }
      

      Can we suggest such a transformation as a Fix-It to help the user along?

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            rwidmann Robert Widmann
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: