Default arguments

I'm trying to use a default argument in a C++ method defined like that:

/// Encapsulates a PDU on the wire.
class PDU {

public:
    PDU();
    const std::vector<uint8_t> frame();

    static PDU ping(std::vector<uint8_t> payload = {});
};

I'm getting

error: missing argument for parameter #1 in call
    let test = CANyonero.PDU.ping()
                                  ^
                                  <#std.__1.vector<UInt8, allocator<UInt8>>#>

Are these not supported yet?

(Side note… what's the best way to allocate this vector from a Swift [UInt8]?)

No one knows?

Default arguments are not supported. You can loop over the array pushing elements back on to the vector:

using Vector = std::vector<UInt8>;
var vector = Vector()
for element in [1, 2, 3] {
  vector.push_back(element)
}

Or if you just want an empty vector:

CANyonero.PDU.ping(Vector())

Related: @egor.zhdan what do you think about providing some kind of extension along with "expressible by array literal" to be able to convert a Swift RandomAccessCollection to a C++ collection? So you could just do Vector(swiftArray)? Might be useful.

1 Like