[C++ and Swift Interoperability] [GSoC 2023] Expanding the Swift Overlay

Hey everyone!

This is Rajveer (GitHub), I am interested in working on the project C++ and Swift interoperability for the ongoing Google Summer of Code 2023!

While my profile would mostly describe me, I will jump straight to the main topic. So, C++ has its existing useful containers like std::map, std::set, std::vector, and many more useful ones which are quite handy while being extremely efficient, and the combination of such package with the existing Swift package would be quite useful. Initially I misunderstood the goal of the project to adding new data structures and basically a whole implementation work dealing with time complexities and adding new containers like Trie, Order Statistic Tree (PBDS) and the addition of existing C++ containers to Swift.

After some research, grinding, surfing and reading through, according to my understanding, I have gone through some useful links which were suggested while I was deeply reading and gaining knowledge about this topic and have gone through the forward Vision of using C++ in Swift while also reading about the progress status of the same. I have also understood that we’re not aiming to implement new C++ types or data structures at this point, instead we’re focusing on making the existing C++ standard library types more convenient to use from Swift (maybe reverse too, but that's probably a discussion for another topic). Going through this String overlay which allows converting a Swift.String to a C++ std::string making the existing type std::string more easily usable from Swift, made it little more clear to what we are exactly aiming to do.

Just to again ensure my clear understanding, let's say that we imported some stdlib container from C++ and tried to loop via Swift, this wouldn't be possible without the overlay, and make the work for developers way harder when working with two or more different libraries of C++ in Swift.

So, I think the next steps would be to get the guidance and mentorship to actually start working and write some good code making a change and providing useful tools to the open source community and existing developers for the ease of use, also making Swift even more functional!

Requesting @egor.zhdan, who is a potential mentor for this project to assist me in making a good proposal for this year, while I will try to deliver my finest and also potentially project new ideas for the same and maybe brush and grind my existing skills and learn even more, and also keeping my active status in contributing towards Swift Compiler.

Thanks,
Rajveer Singh Bharadwaj

6 Likes

Hi Rajveer,

Yes, this is correct. Swift allows iterating over an instance of a type with for-in loop if the type conforms to Swift.Sequence protocol. The overlay defines CxxRandomAccessCollection protocol, which inherits from Sequence, and the Swift compiler automatically conforms C++ container types such as std::vector to CxxRandomAccessCollection whenever possible.

Not sure what you mean by this, could you elaborate a bit?

Hey Egor,

So what I basically meant is, without the overlay, since the for-in-loop is not accessible, consider a complex type that a developer made, such as, std::vector<std::map<int, std::set>>>, or something similar to any of the containers in combination, while a usual C++ loop would look something like this:

(for above example)

for (auto& x: v) {
  for (auto& [y, c]: x) {
    for (auto& z: c {
      //do something
    }
  }
}

This is what I meant, and also, Swift offers flexibility of using for-in-where-loop that makes the code even more readable for any developer, although, it might be quite simple, for a daily task, one would always try to make the code little cleaner and easier to implement, so I meant my statement in that way.

[There might me more cases, which I am probably missing to explain...]

Thanks for your reply!