-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Symmetrically enforce the types of arguments to Boolean operators #9711
Comments
I agree with your assessment that there is no good solution to this :) |
Renaming |
I certainly agree that it is troublesome that we have |
If |
One would only be defined for |
Removing the short circuiting nature of the && and || operators will make protecting expressions that are erroneous in some conditions much more verbose. Programmers have long erroneously called them Boolean operators but, as you say, they are flow control and should be defined in that part of the manual. Instead the |
I agree with that; I have wished several times that |
Although if we're going to reclaim those operators, merely having them be function versions of |
Given how often I've ended up in convert errors, part of me is a little ERROR: That does open the door to truthiness though, which is definately a mixed On Sat, Jan 10, 2015 at 9:17 AM, John Myles White [email protected]
|
If Otherwise, it seems like the pattern to encourage would be x = foo()::Nullable{Bool}
y = get(x,false) #or whatever default 2nd arg makes sense
y && continue_on_with_life() I think that falls under your 3rd option? I'm not well-versed in the functionality/use of 3-valued logic, so perhaps that's why my initial reaction is to enforce |
Maybe another, less disruptive solution: try to do type "light" inference on the right-hand side without evaluating. So Regarding the debate around operators, the previous reference is #5238 |
@quinnj: The main example where three-valued logic comes up is the evaluation of WHERE predicates in SQL (and their analogues in DataFrames indexing) where you want to include rows that match some predicates. There is also where things are most confusing about the current batch of Boolean special-forms + functions: you could safely do something like, inds = Int[]
for i in 1:size(df, 1)
if df[i, 1] | df[i, 2]
push!(inds, i)
end
end but this will break with a change to It's exacerbated by the strong surface similarity of |
I used to be fond of the Ada syntax for |
Erlang has |
I don't think this is a bug. |
Very related here is that the precedence of |
I'm sorry about this, but julia cannot give a type error about an expression without evaluating it, so this will have to stay as-is. |
Right now, we have a partial implementation of three-valued logic because the left-hand side of short-circuiting Boolean operators is checked for Bool typing at run-time, but the right-hand side is not checked for Bool typing if the operation short-circuits, since this would requiring evaluating the right-hand expression. This produces particularly odd results when you introduce
Nullable{Bool}()
into the mix, because you end up with things like:I don't see any unambiguously correct solution to this given Julia's dynamic typing and the design objectives of Boolean operators in Base Julia, but there are some possible approaches:
||
and&&
into a special evaluation rule that can be extended via dispatch. This has the undesirable consequence of allowing people to add truthiness to the language via possible extensions to Boolean operations.Nullable
arguments to||
and&&
so that we can implement three-valued logic in full.||
and&&
combined with run-time typing means that some cases will not raise errors despite being incorrect uses of||
and&&
by convention.Obviously, this all interacts with the Julia idiom of doing control-flow via Boolean operations.
The text was updated successfully, but these errors were encountered: