-
Notifications
You must be signed in to change notification settings - Fork 255
[cxx-interop] Add documentation about calling ctor or static factory of SWIFT_SHARED_REFERENCE types as Swift Initializer #1079
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1239,7 +1239,7 @@ To specify that a C++ type is a shared reference type, use the `SWIFT_SHARED_REF | |||||
| class SharedObject : IntrusiveReferenceCounted<SharedObject> { | ||||||
| public: | ||||||
| SharedObject(const SharedObject &) = delete; // non-copyable | ||||||
|
|
||||||
| SharedObject(); | ||||||
| static SharedObject* create(); | ||||||
| void doSomething(); | ||||||
| } SWIFT_SHARED_REFERENCE(retainSharedObject, releaseSharedObject); | ||||||
|
|
@@ -1250,11 +1250,41 @@ void releaseSharedObject(SharedObject *); | |||||
|
|
||||||
| Now that `SharedObject` is imported as a reference type in Swift, the programmer will be able to use it in the following manner: | ||||||
| ```swift | ||||||
| let object = SharedObject.create() | ||||||
| object.doSomething() | ||||||
| // The C++ constructor is imported as a Swift initializer | ||||||
| let object1 = SharedObject.create() | ||||||
| let object2 = SharedObject() | ||||||
| object1.doSomething() | ||||||
| object2.doSomething() | ||||||
| // `object` will be released here. | ||||||
| ``` | ||||||
|
|
||||||
| You can create instances of `SharedObject` directly from Swift by calling its C++ constructor through a Swift initializer. | ||||||
fahadnayyar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
fahadnayyar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| **NOTE:** Swift uses *default* `new` operator for constructing c++ shared reference types. If you'd like to prevent Swift from importing constructors as initializers, you can also delete the *default* `new` operator in C++. | ||||||
fahadnayyar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
fahadnayyar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
|
|
||||||
| Alternatively, you can also construct instances of `SharedObject` using a user-defined static factory function, provided that the factory function is annotated with `SWIFT_NAME("init(...)")`, where the number of `_` placeholders matches the number of parameters in the factory function. | ||||||
fahadnayyar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| For example, consider a factory that performs custom allocation or returns a singleton instance: | ||||||
fahadnayyar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| ```cpp | ||||||
| struct SharedObject { | ||||||
| static SharedObject* make(int id) SWIFT_NAME("init(_:)"); | ||||||
fahadnayyar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| void doSomething(); | ||||||
| } SWIFT_SHARED_REFERENCE(retainSharedObject, releaseSharedObject); | ||||||
| ``` | ||||||
|
|
||||||
| In this case, Swift will import the static `make` function as a Swift initializer: | ||||||
|
|
||||||
| ```swift | ||||||
| let object = SharedObject(id: 42) | ||||||
|
||||||
| let object = SharedObject(id: 42) | |
| let object = SharedObject(42) |
But I think you should give more examples here. Also, doSomething() isn't really necessary here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@j-hui Let's add more examples of using the SWIFT_NAME technique in a follow-up patch. We can experiment with this feature on some adopter projects or dummy examples before documenting.
Uh oh!
There was an error while loading. Please reload this page.