Repository navigation
Refactor explicit operator bool usage for some cases - #8601
Conversation
valid() over explicit operator bool()
valid() over explicit operator bool()54ba98f to
b411b8b
Compare
There was a problem hiding this comment.
So, to add some additional nuance to #8562 (comment), not all our uses of operator bool() are implementing a validity check (though some of them definitely are). I would distinguish:
- Use of member data sentinel values to create an ad hoc implementation of an "absent" state, e.g. in
PositionedIcon,Shaping,Glyph,GlyphMetrics. These should typically be replaced with uses ofoptional<T>and elimination of the "invalid" state from the inner type. - Types which are acting similarly to a pointer or optional type, e.g.
ExtensionFunction,CFHandle. These could arguably useoptionaltoo, but sometimes it's not convenient. Emulating a pointer by providingoperator bool()(and usuallyoperator*as well) is reasonable. Result. This is in its own category because it's a fundamental type, on the level ofoptional. Providingoperator bool()is necessary for error-handling ergonomics.- Types which have some other special state, but where that state is considered a valid member of the type's domain. These should typically use a named method specific to the state, e.g.
TransitionOptions::present()orPropertyValue::defined(). (Note that the most natural name might name the complement of the special state -- all members of the type's domain other than the special state.)
| const CameraFunction<T>& asCameraFunction() const { return value.template get<CameraFunction<T>>(); } | ||
|
|
||
| explicit operator bool() const { return !isUndefined(); }; | ||
| bool valid() const { return !isUndefined(); }; |
There was a problem hiding this comment.
defined or present would be better; an undefined property value isn't invalid.
There was a problem hiding this comment.
This is actually not used, it seems isUndefined() suits the purpose already.
| } | ||
|
|
||
| explicit operator bool() const { | ||
| bool valid() const { |
| } | ||
|
|
||
| constexpr explicit operator bool() const { | ||
| constexpr bool valid() const { |
There was a problem hiding this comment.
Reverse the sense and call it empty or zero instead?
| using variant<T, Error>::variant; | ||
|
|
||
| explicit operator bool() const { | ||
| bool valid() const { |
There was a problem hiding this comment.
I think this is a reasonable use of operator bool and doesn't need to change. Of course, we should probably follow d7227e1 and eliminate this copy of Result entirely.
|
|
||
| Statement::operator bool() const { | ||
| return impl.operator bool(); | ||
| bool Statement::valid() const { |
| } | ||
|
|
||
| explicit operator bool() const { | ||
| bool valid() const { |
There was a problem hiding this comment.
I think this is a valid use of operator bool and doesn't need to change. ExtensionFunction acts like a function pointer.
| index(index_), | ||
| hasText(shapedTextOrientations.first || shapedTextOrientations.second), | ||
| hasIcon(shapedIcon), | ||
| hasText(shapedTextOrientations.first.valid() || shapedTextOrientations.second.valid()), |
There was a problem hiding this comment.
A better fix here is likely to use an optional.
|
|
||
| struct GlyphMetrics { | ||
| explicit operator bool() const { | ||
| bool valid() const { |
There was a problem hiding this comment.
If "all 0" is being used as a proxy for "no metrics available", a better change is to use optional<GlyphMetrics>.
|
|
||
| explicit operator bool() const { | ||
| return metrics || rect.hasArea(); | ||
| bool valid() const { |
There was a problem hiding this comment.
Same here -- optional<Glyph> instead.
|
@jfirebaugh let's wait for #3802 and #8562 to land, so I can rebase and address the review comments in one step. |
a9c52fd to
e677563
Compare
e677563 to
e84594d
Compare
e84594d to
f3f7ec0
Compare
|
@jfirebaugh I've applied the requested changes based on your comments - thank you for the detailed explaining on these! The only exception is |
|
|
||
| // Glyph metrics | ||
| GlyphMetrics metrics; | ||
| optional<GlyphMetrics> metrics; |
There was a problem hiding this comment.
Once Glyph is optional, GlyphMetrics::operator bool() is unused, so we don't need to make this optional.
There was a problem hiding this comment.
I have an alternate fix for glyph-related operator bool() prepared as part of #8747, so I'll omit the last commit here and merge.
f3f7ec0 to
04fb715
Compare
No description provided.