From: dan.garubba@... Date: 2018-07-30T19:57:44+00:00 Subject: [ruby-core:88214] [Ruby trunk Feature#14951] New operator to evaluate truthy/falsy/logical equivalence Issue #14951 has been updated by danga (Dan Garubba). Sure. In my day job, I write testing code. So I've written an expression like: ~~~ ruby raise MyError unless in_scenario_x? == actions_performed_for_scenario_x? ~~~ To express: "raise an error unless the actions are performed for scenario X if and only if we are in scenario X". However, when I implemented this, I got unexpected behavior since I was expecting boolean singleton operands for my `==` operator, but found that wasn't the case. I had found a non-boolean singleton operand from a `Numeric#nonzero?` call propagated in the underlying logic of `actions_performed_for_scenario_x?`, so I had to re-implement with proper type handling to ensure boolean singleton operands. If I had an operator for truthy-equivalence (not a proposal, but for this example: `iff`, since `=?` has compatibility issues) I could write something like: ~~~ ruby raise MyError unless in_scenario_x? iff actions_performed_for_scenario_x? ~~~ So that I would only care about the truthiness of the predicate methods, and not the specific return types. From a propositional calculus perspective, Ruby's truthy/falsy semantics seem to work just fine for the fundamental logical operations of negation, disjunction, and conjunction. But logical equivalence requires an extra degree of care for type handling when expressed using the `==` operator. There are workarounds, like the aforementioned `!!a == !!b`. But I wanted to see if the logical equivalence use case was strong enough for its own operator, or if everyone was OK living with the workarounds instead. ---------------------------------------- Feature #14951: New operator to evaluate truthy/falsy/logical equivalence https://bugs.ruby-lang.org/issues/14951#change-73234 * Author: danga (Dan Garubba) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- I propose adding a new operator for truthy/falsy equivalence, similar to what was proposed on https://bugs.ruby-lang.org/issues/13067, but with new syntax. The main purpose would be for writing expressions for logical equivalence (i.e., "if and only if" relationships) that only considers the truthiness the operands. Since predicate methods like `File#size?` and operators like `=~` follow truthy semantics without returning the `true` and `false` singletons, using them in logical expressions that evaluate for logical equivalence can be error-prone without the proper return type awareness and conversions. This proposed operator would be equivalent to `!!a == !!b`, but I feel that a new operator would be more concise and more expressive of the concept of logical equivalence. Attached is a prototype implementation of the operator as '=?'. ---Files-------------------------------- teq.patch (3.47 KB) -- https://bugs.ruby-lang.org/ Unsubscribe: