Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tqchen committed Apr 13, 2020
1 parent 4942219 commit ebba40d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion include/tvm/ir/attrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ namespace tvm {
template<typename TObjectRef>
inline TObjectRef NullValue() {
static_assert(TObjectRef::_type_is_nullable,
"Can only value for nullable types");
"Can only get NullValue for nullable types");
return TObjectRef(ObjectPtr<Object>(nullptr));
}

Expand Down
7 changes: 7 additions & 0 deletions include/tvm/runtime/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,13 @@ class Optional : public ObjectRef {
CHECK(data_ != nullptr);
return T(data_);
}
/*!
* \return The contained value if the Optional is not null
* otherwise return the default_value.
*/
T value_or(T default_value) const {
return data_ != nullptr ? T(data_) : default_value;
}
/*! \return Whether the container is not nullptr.*/
explicit operator bool() const {
return *this != nullptr;
Expand Down
6 changes: 4 additions & 2 deletions tests/cpp/container_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ TEST(Optional, Composition) {
CHECK(opt1 == "xyz");
CHECK(opt1 != nullptr);
CHECK(opt0 == nullptr);
CHECK(opt0.value_or("abc") == "abc");
CHECK(opt1.value_or("abc") == "xyz");
CHECK(opt0 != opt1);
CHECK(opt1 == Optional<String>(String("xyz")));
CHECK(opt0 == Optional<String>(nullptr));
Expand Down Expand Up @@ -449,8 +451,8 @@ TEST(Optional, PackedCall) {
return s;
};
auto func = TypedPackedFunc<Optional<String>(Optional<String>, bool)>(tf);
func(String("xyz"), false);
func(Optional<String>(nullptr), true);
CHECK(func(String("xyz"), false) == "xyz");
CHECK(func(Optional<String>(nullptr), true) == nullptr);

auto pf = [](TVMArgs args, TVMRetValue* rv) {
Optional<String> s = args[0];
Expand Down

0 comments on commit ebba40d

Please sign in to comment.