bkietz commented on code in PR #39937:
URL: https://github.com/apache/arrow/pull/39937#discussion_r1478439624
##########
cpp/src/arrow/datum.cc:
##########
@@ -73,86 +74,108 @@ std::shared_ptr<Array> Datum::make_array() const {
}
const std::shared_ptr<DataType>& Datum::type() const {
- if (this->kind() == Datum::ARRAY) {
- return std::get<std::shared_ptr<ArrayData>>(this->value)->type;
- }
- if (this->kind() == Datum::CHUNKED_ARRAY) {
- return std::get<std::shared_ptr<ChunkedArray>>(this->value)->type();
- }
- if (this->kind() == Datum::SCALAR) {
- return std::get<std::shared_ptr<Scalar>>(this->value)->type;
- }
- static std::shared_ptr<DataType> no_type;
- return no_type;
+ return std::visit(
+ [](const auto& value) -> const std::shared_ptr<DataType>& {
+ using T = std::decay_t<decltype(value)>;
+
+ if constexpr (std::is_same_v<T, std::shared_ptr<Scalar>> ||
+ std::is_same_v<T, std::shared_ptr<ArrayData>>) {
+ return value->type;
+ } else if constexpr (std::is_same_v<T, std::shared_ptr<ChunkedArray>>)
{
+ return value->type();
+ } else {
+ static std::shared_ptr<DataType> no_type;
+ return no_type;
Review Comment:
The suggestion results in returning a reference to temporary; unless we
replace the return-by-reference with return-by-value a static variable is
necessary.
We use this pattern in a couple places, I've considered providing a helper
which would make it easier:
```suggestion
return kDefaultRef;
```
```
struct {
template <typename T>
operator const T&() const {
static const T instance;
return instance;
}
} constexpr kDefaultRef;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]