Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[swift2objc] Support closures #1669

Open
liamappelbe opened this issue Oct 21, 2024 · 2 comments
Open

[swift2objc] Support closures #1669

liamappelbe opened this issue Oct 21, 2024 · 2 comments

Comments

@liamappelbe
Copy link
Contributor

Support functions and methods that accept and return closures:

class Foo {
  func bar(callback: (Int) -> Int) {
    // ...
  }
}
@nikeokoronkwo
Copy link
Contributor

Closure parsing via swiftc doesn't work or isn't as clear as it is for other types in Swift. Same goes for tuples (which makes picking out closures much more difficult)

For the given variables:

// tuple
public var myTuple = (9)

// closure
public var myClosure = { (o: Int) -> Int in return 9 }

swiftc generates the following symbol graph for them

{
  "kind": {
      "identifier": "swift.var",
      "displayName": "Global Variable"
  },
  "identifier": {
      "precise": "s:28primitive_symbolgraph_module7myTupleSivp",
      "interfaceLanguage": "swift"
  },
  "pathComponents": [
      "myTuple"
  ],
  "names": {
      "title": "myTuple",
      "subHeading": [
          {
              "kind": "keyword",
              "spelling": "var"
          },
          {
              "kind": "text",
              "spelling": " "
          },
          {
              "kind": "identifier",
              "spelling": "myTuple"
          },
          {
              "kind": "text",
              "spelling": ": ("
          },
          {
              "kind": "typeIdentifier",
              "spelling": "Int",
              "preciseIdentifier": "s:Si"
          },
          {
              "kind": "text",
              "spelling": ")"
          }
      ]
  },
  "declarationFragments": [
      {
          "kind": "keyword",
          "spelling": "var"
      },
      {
          "kind": "text",
          "spelling": " "
      },
      {
          "kind": "identifier",
          "spelling": "myTuple"
      },
      {
          "kind": "text",
          "spelling": ": ("
      },
      {
          "kind": "typeIdentifier",
          "spelling": "Int",
          "preciseIdentifier": "s:Si"
      },
      {
          "kind": "text",
          "spelling": ")"
      }
  ],
  "accessLevel": "public",
  "location": {
      "uri": "file://types.swift",
      "position": {
          "line": 23,
          "character": 11
      }
  }
},
{
  "kind": {
      "identifier": "swift.var",
      "displayName": "Global Variable"
  },
  "identifier": {
      "precise": "s:28primitive_symbolgraph_module9myClosureyS2icvp",
      "interfaceLanguage": "swift"
  },
  "pathComponents": [
      "myClosure"
  ],
  "names": {
      "title": "myClosure",
      "subHeading": [
          {
              "kind": "keyword",
              "spelling": "var"
          },
          {
              "kind": "text",
              "spelling": " "
          },
          {
              "kind": "identifier",
              "spelling": "myClosure"
          },
          {
              "kind": "text",
              "spelling": ": ("
          },
          {
              "kind": "typeIdentifier",
              "spelling": "Int",
              "preciseIdentifier": "s:Si"
          },
          {
              "kind": "text",
              "spelling": ") -> "
          },
          {
              "kind": "typeIdentifier",
              "spelling": "Int",
              "preciseIdentifier": "s:Si"
          }
      ]
  },
  "declarationFragments": [
      {
          "kind": "keyword",
          "spelling": "var"
      },
      {
          "kind": "text",
          "spelling": " "
      },
      {
          "kind": "identifier",
          "spelling": "myClosure"
      },
      {
          "kind": "text",
          "spelling": ": ("
      },
      {
          "kind": "typeIdentifier",
          "spelling": "Int",
          "preciseIdentifier": "s:Si"
      },
      {
          "kind": "text",
          "spelling": ") -> "
      },
      {
          "kind": "typeIdentifier",
          "spelling": "Int",
          "preciseIdentifier": "s:Si"
      }
  ],
  "accessLevel": "public",
  "location": {
      "uri": "file://types.swift",
      "position": {
          "line": 26,
          "character": 11
      }
  }
}

Apparently, it doesn't play so well with types containing characters other than literal characters (i.e symbols).

@liamappelbe
Copy link
Contributor Author

Ok. Looks like we'll have to write a bit of custom parsing logic for this.

@liamappelbe liamappelbe added this to the SwiftGen MVP milestone Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

No branches or pull requests

2 participants