I'm working with a C++ library that has methods taking std::optionals (sometimes multiple std::optional parameters in a row).
I noticed that there isn't really any built-in way to build a c++ optional in swift. Since there are no initialisers taking a value (there are only those taking nullopt_t or nilLiteral) would have expected some way to assign the value from Swift, either by assigning it to the variable itself, or by assigning to .value
but the former fails due to different Types, while the latter fails due to .value
being read-only. I thought that assigning to .pointee
was the way, but it doesn't actually work (see GitHub - Drejzer/swift-std.optional-minimal: Minimal example for std.optional assignment from swift ).
The only method to pass a non-nil std.optional to a cpp function that I've found is to construct the optional in cpp (by calling make_optional within cpp, since it doesn't seem to work from swift)
Having a bunch of "makeOptional" methods feels clunky, though is probably better that having a bunch on overloads of functions that explicitly take nullopt_t as one of the arguments.
Has there been any consideration for such approach? Or are there some significant problems with this approach? Or am I missing something painfully obvious?
(I was planning to create an issue on GitHub, but was pointed here by the template.)