imba icon indicating copy to clipboard operation
imba copied to clipboard

Predicate definitions with Arity above 0 should be able to end in a '?' without generating a warning to maintain consistency with simple boolean return value getters.

Open peter-wasilko opened this issue 3 years ago • 3 comments

Imba Version: 2.0.0-alpha.202

Details

In trying to define a predicate to return whether a collection contains a reference to an element, I want to end the function name with a question mark to flag its boolean result type (ie. invoke the predicate with : collection.contains? element)

Expected Behavior

This should work without generating a warning unless the function definition doesn't return a boolean value. Otherwise, someone reading imba code will not be able to rely on the question mark as a marker for all predicates.

Restricting the '?' notation to getters/setters seems arbitrary and counter intuitive and would require a preceding function call to store any arguments in an element's properties or blackboard construct before invoking an arity 0 getter proper. (e.g. element.test-collecition = $list followed by element.in-test-collection?)

Actual Behavior

The code runs but the compiler warns:

        warning: [imba] Only getters/setters should end with ?

Possible Fix

If the compiler can not infer that a boolean is being return, change the warning message to:

        warning: [imba] Cannot infer a boolean return value, only functions returning true or false should end with ? If that is the case, you may safely disregard this warning.

Otherwise, permit getters to accept arguments.

peter-wasilko avatar Mar 09 '22 18:03 peter-wasilko

I see. I've always thought that predicates should not support arguments, mostly from an aesthetic viewpoint (with parens it is easy to misread as a ternary etc item.render?(something)). But I see the argument against this. Another thing I'm worried about is that if people define predicates as methods, item.contains? will always be truthy since it is just a reference to a function, and this may be very hard to catch.

My proposal would be to still allow them as methods, but warn (or disallow) predicate methods without parameters. What do you think?

somebee avatar Mar 10 '22 05:03 somebee

I think that is an ideal solution, since a parameterless predicate should be expressed as a getter rather than a method.

peter-wasilko avatar Mar 11 '22 06:03 peter-wasilko

@haikyuu Let's implement this together and ship it as part of the next alpha tomorrow!

somebee avatar Mar 24 '22 21:03 somebee