Macro to unify two types

Let's say I have two types:

struct A {
    let a: Int
}
struct B {
    let b: Int
}

I would like to have a macro that unifies them into one type:

@Magic(A, B)
struct MyNewType {}

which will expand into something like:

struct MyNewType {
    let a: Int?
    let b: Int?
}

My question is, is this possible with Swift macros? If so, which macro rule should be preferred?

Thanks in advance!

I don't think it's possible with Swift macro. The main reason is that, since Magic is attached to MyNewType, it has no access to the members of A and B. Swift macro is designed to support only a few selected scenarios. It can't transform code in arbitrary way.

P.S. it's possible if you want to generate A and B from MyNewType.

1 Like