Skip to content

Overload get() function for Optional type. - #9748

Merged
junrushao merged 7 commits into
apache:mainfrom
yzh119:optional-get
Feb 16, 2022
Merged

junrushao merged 7 commits into
apache:mainfrom
yzh119:optional-get

Conversation

@yzh119

@yzh119 yzh119 commented Dec 15, 2021

Copy link
Copy Markdown
Member

Currently Optional don't have its own get() implementation, so if we want to get the internal container pointer of an optional object o with type Optional<T>, we can either use o.value().get() (in the case it's defined) or static_cast<const T*>(o.get()) (because the inherited get() function has no type information). Neither of them are satisfactory if we want to get a pointer with type const T*.

This PR overloads the get() function of Optional, which returns nullptr when the object is not defined, and it's corresponding data pointer otherwise.

cc @junrushao1994 @tqchen

@junrushao

Copy link
Copy Markdown
Member

Why close this PR? I'm very interested

@yzh119

yzh119 commented Dec 15, 2021 via email

Copy link
Copy Markdown
Member Author

Comment thread include/tvm/runtime/container/optional.h Outdated
@yzh119

yzh119 commented Jan 20, 2022

Copy link
Copy Markdown
Member Author

I think the CI was broken because of some random numerical issue and I'll re-trigger it, @junrushao1994 any further comments on this PR?

@junrushao

Copy link
Copy Markdown
Member

Yes, it's a bit of flaky. Let me retrigger this

@junrushao

Copy link
Copy Markdown
Member

@yzh119 Would you like to rebase and retrigger the CI? Thanks!

@yzh119

yzh119 commented Feb 11, 2022

Copy link
Copy Markdown
Member Author

@junrushao1994 done.

* \return The internal object pointer with container type of T.
* \note This function do not perform not-null checking.
*/
const ContainerType* get() const { return static_cast<ContainerType*>(data_.get()); }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that get is not a standard API as per stl style for Optional. While get is a standard stl style name. We might want to have a longer name in this case.

How about GetContainerPtr(use CamelCase to indicate that it is not STL compatible).

The comment can be updated to explain the behavior of nullopt case(return nullptr) and non-nullopt case(returns the container ptr)

@junrushao junrushao Feb 16, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @tqchen for the valuable feedback! On the other hand, I would say this PR actually does is to simply improve the Optional::get method to return the underlying type instead of the plain Object pointer, and hence it might make sense to keep the name as it is. What do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I didn't noticed that ObjectRef also have a get method. I think this sounds good to me then. Perhaps we can just add additional comments about the behavior

@junrushao

Copy link
Copy Markdown
Member

Thanks @tqchen @yzh119!

@junrushao
junrushao merged commit 1da0093 into apache:main Feb 16, 2022
electriclilies pushed a commit to electriclilies/tvm that referenced this pull request Feb 18, 2022
* upd

* simplify

* upd

* fix

* upd

* fix docstring
pfk-beta pushed a commit to pfk-beta/tvm that referenced this pull request Apr 11, 2022
* upd

* simplify

* upd

* fix

* upd

* fix docstring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants