masters3d
(Masters3d)
December 24, 2023, 2:30am
1
I am using a func that is coming in as mutating func SetPosition(_ x: Double, _ y: Double, _ z: Double) -> scad.Key
I want to expose swifty version like so
mutating func setPosition(x: Double, y: Double, z: Double)
Is this possible using macros?
I can do it manually something like but I am hoping I can just apply a macro and I can do it across many other functions
extension MyType {
mutating func setPosition(x: Double, y: Double, z: Double) {
this.SetPosition(x,y,z)
}
}
Yes, that’s possible with macros. Have a look into extension
and/or member
macros. Both allow you to add methods to existing declarations.
However, you need to have access to the declaration defining the SetPosition
function. The macro needs to be attached to it.
If you're using an extension macro, you may run into the issue where the contents of the extension will be effectively fileprivate
.
opened 05:14PM - 09 Oct 23 UTC
bug
swift macro
**Description**
When using an attached ExtensionMacro to add an extension met… hod to a protocol, that extension method is only visible in the file where the Macro attribute is applied, even when it should be internally visible by default (or following other explicit access control levels).
If I have all this in the same file it works.
```
import MacroLibrary
@FooExtension // <--- apply the macro to generate default `Foo` property and `printFoo()` method
protocol Foo {
var foo: String { get }
}
extension String: Foo { }
"Test".printFoo() // prints "foo"
```
But if I have the last line in a separate file, the extension method isn't visible even if I generated it specifically with an internal modifier
```
// Some other file that is different from the above
"Test".printFoo() // <---- Error: "Value of type ''String" has no member 'printFoo'"
```
I suspect existing test cases for attached ExtensionMacros may have trouble catching this issue since the extension method is called in the same test file where the macro is applied (therefore never triggering the visibility issue). I'm attached a minimal example package here to demonstrate.
[MyMacro.zip](https://github.com/apple/swift/files/12848688/MyMacro.zip)
**Steps to reproduce**
* Create an attached ExtensionMacro that adds an extension method to the protocol declaration it is attached to
* Annotate a protocol with the ExtensionMacro in some client code. Note that the extension method can be called in the same file
* In another file in the client code, try to call the same extension method
**Expected behavior**
I expect the extension method to be visible, autocomplete-suggested, and called at runtime. However, the compiler says the method doesn't exist.
<img width="1732" alt="Screenshot 2023-10-09 at 11 01 56 AM" src="https://github.com/apple/swift/assets/8276529/518345e1-a2ae-4df8-8031-2ab02af9bfce">
**Environment**
- Swift compiler version info : `swift-driver version: 1.87.2 Apple Swift version 5.9 (swiftlang-5.9.2.1.6 clang-1500.1.0.1.1)`
- Xcode version info: `Xcode 15.1 Build version 15C5028h`
- Deployment target: iOS 17
masters3d
(Masters3d)
December 24, 2023, 7:49pm
4
I have access to it but that declaration is in a cpp header file (not swift). In effect I want to change the behavior of the swift versions of the cpp declarations by using macros.
Well, I don’t see a way to extend this type with the help of macros then. Macros exclusively work on the syntax of the declaration they are attached to. Your methods are declared in a C++ header file, though. So there is no way for any macro to access them.