17645, "bradcray", "Should user-defined types be able to used in conditionals? And if so, how?", "2021-05-06T00:11:58Z"
As a library author, I may want to define a type that would be convenient to use in a conditional. As a somewhat artificial example, if I define a type:
record height {
var inches: int;
}
I may want to permit a user to write:
if child.height then
rather than:
if child.height.inches > 0 then // or `if child.height.getHeightInInches() > 0` or ...
In the modules developed by the core team, we have benefitted from this capability in the Regex
module, for c_ptr
, and for the syserr
type, all of which are defined within library modules. Should we extend this capability to users as well?
Pros:
- supports creating the ability to create types with succinct conditional forms
Cons:
- like operator overloads, could be subject to abuse
- somewhat similar to an implicit conversion, which some users have been averse to due to things happening invisibly
If we were to support this, how would we express it?
- a well-defined method on the type?
- a (new) operator on the type?
In either case, what would the name be?
Related issues:
- Should conditional expressions be restricted to booleans? · Issue #14930 · chapel-lang/chapel · GitHub
- isTrue, short-circuiting exprs and conditional exprs/stmts · Issue #15904 · chapel-lang/chapel · GitHub
- `import Regex` breaks ability to write `if myre.match(...)` · Issue #17633 · chapel-lang/chapel · GitHub