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

.self can be omitted if a function has only one parameter

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:

      Description

      I'm wondering what the reasoning is for allowing Types to be passed into a function without explicitly declaring .self if there is only one function parameter.

      When ommitting .self:

      • The function has 1 parameter: compiles
      • The function has > 1 parameter: Expected member name or constructor call after type name

      Take the following code:

      Issue
      func test<T: Any>(type: T.Type, two: String) {
          print(type)
      }
      
      func test<T: Any>(type: T.Type) {
          print(type)
      }
      
      test(Int.self) //no error
      test(Int) //no error
      
      test(Int.self, two: "") //no error
      test(Int, two: "") //error: Expected member name or constructor call after type name
      

      To give some background, I am trying to create a function with the signature

      Signature
      func get<T: Any>(path: String, _ star: T.Type, handler: (Request, T) throws -> Response)
      

      That can be used like so

      Example
      app.get("users", Int) { request, id in 
         print("You requested user with ID \(id)")
         ...
      }
      

      This get function works if you include .self after all of the Types, but that is not as clean and seems unnecessary unless I'm missing something.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              tannernelson tanner0101
              Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: