Hi @tongjiew, and welcome to the community! Thanks for voicing out your interest in this.
I can provide you with some additional insight into what this project entails. I am working on teaching the Swift compiler how to generate a C++ header that is able to represent Swift types , so that they can be used from C++. Swift enumerations will be represented using a C++ class
type, with the underlying enum value being stored inline, or boxed up on the heap, depending on the layout of the Swift type. Enumerations without associated values will have a case value that's accessible from the class itself. For example, a Swift enum CompassDirection
:
enum CompassDirection {
case north
case south
case east
case west
}
will be mapped to the following C++ class with an interface similar to this one:
class CompassDirection {
public:
static const CompassDirection north;
static const CompassDirection south;
static const CompassDirection east;
static const CompassDirection west;
enum class cases { north, south, east, west };
cases operator cases() const { ... }
};
which could then be used from C++ in a switch like this:
CompassDirection getOpposite(CompassDirection cd) {
switch (cd) { // implicit conversion to CompassDirection::cases
using enum CompassDirection::cases; // allow name lookup to find enum cases.
case north:
return CompassDirection::south;
case south:
return CompassDirection::north;
case east:
return CompassDirection::west;
case west:
return CompassDirection::east;
}
}
This approach would need to be extended to work with enums that have associated values. For example, it would be reasonable to map a Swift enum like this:
enum Barcode {
case upc(Int, Int, Int, Int)
case qrCode(String)
}
To a C++ class like this one:
class Barcode {
public:
Barcode() = delete;
enum class cases { upc, qrCode };
operator cases() const { ... }
using UpcType = swift::Tuple<swift::Int, swift::Int, swift::Int, swift::Int>;
// Returns true if the Swift enumeration is of case Barcode.upc
bool isUpc() const;
// Extracts the associated valus from Barcode.upc enum case
UpcType getUpc() const;
// Returns true if the Swift enumeration is of case Barcode.qrCode
bool isQrCode() const;
// Extracts an associated value from Barcode.qrCode enum case
swift::String getQrCode() const;
static Barcode initUpc(swift::Int, swift::Int, swift::Int, swift::Int);
static Barcode initQrCode(swift::String);
};
The goal of this specific GSoC project then would be to extend the C++ header generator in Swift to emit C++ class
declarations (with implementation) for enums with associated values, like in the Barcode
example above.
If you're interested in this project, I think the next step that GSoC suggests is to start working on writing a project proposal based on this idea. Feel free to post your proposal draft on the forums, so that we can provide some feedback for you to help your proposal. In addition to that, I would also recommend reading through getting started docs for Swift, to get a sense of how you can setup a working environment for you to work on the Swift compiler:
Cheers,
Alex