Skip to content
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

! for infix operators #25512

Open
DNF2 opened this issue Jan 11, 2018 · 10 comments
Open

! for infix operators #25512

DNF2 opened this issue Jan 11, 2018 · 10 comments
Labels
parser Language parsing and surface syntax

Comments

@DNF2
Copy link

DNF2 commented Jan 11, 2018

It would be nice to be able to negate infix operators by writing

a !<op> b

and have that be equivalent to

!(a <op> b)

This is analogous to how !<function> works, and it also works for operators called in function form, like this:

julia> !<(3, 5)
false

Also allowing infix negation seems more consistent.

Related discussion is here

Many unicode operators have negated versions, but not all. Furthermore, negated unicode operators frequently are rendered as small and hard to read compared to surrounding code. As for ascii operators, I miss in particular:

a !in A
b !isa B
C !<: D

The unicode version of !in is , but mixing in and in code isn't very pleasing to the eye. And having ! compose with arbitrary infix operators means that they will always work, instead of depending on whether or not a suitable negated unicode symbol happens to be available.

@JeffBezanson JeffBezanson added the parser Language parsing and surface syntax label Jan 11, 2018
@StefanKarpinski
Copy link
Member

I generally like this idea but there are some consistency issues. For example, if a !<op> b generally means !<op>(a, b) then that should imply that !== would mean what != actually means whereas !== is actually the negation of ===. Aside from a few such inconsistencies, it's a really nice idea that dovetails well with the fact that !f is already a version of f negated.

Side note: interesting tie-in with #25435. If we merge that then !f is no longer restricted to boolean negation of a function – it actually means bitwise negation of the result of f.

@yurivish
Copy link
Contributor

yurivish commented Jan 12, 2018

There might be a more general feature in this general area that would allow unary operators to operate on other operators, similar to adverbs in J. Reminds me a bit of Julia's vectorizing ., too...

@vtjnash
Copy link
Member

vtjnash commented Jan 12, 2018

Side note: interesting tie-in with #25435. If we merge that then !f is no longer restricted to boolean negation of a function – it actually means bitwise negation of the result of f.

We could resolve that by adding a type-assert

@StefanKarpinski
Copy link
Member

We could resolve that by adding a type-assert

I suggested the same on the other thread, but it's a bit weird and arbitrary.

@samoconnor
Copy link
Contributor

#15353

@logankilpatrick
Copy link
Member

Bumping this up after I spent 30 minutes yesterday trying to figure out the right way to do "not in" and realized the best way right now is to do !(a in b) which isn't terrible but I actually tried !in and got an error.

@dhanak
Copy link

dhanak commented May 6, 2021

Bumping this up after I spent 30 minutes yesterday trying to figure out the right way to do "not in" and realized the best way right now is to do !(a in b) which isn't terrible but I actually tried !in and got an error.

Have you considered ∉ (\notin)? This is one case where I prefer using unicode operators. (In fact, I use ∈ to test for inclusion and "in" for for loops and generators, so the ∉ operator comes perfectly natural to me.)

@cossio
Copy link
Contributor

cossio commented Sep 8, 2022

You don't have the equivalent of ∉ for other infix ops. For example isa, <:, for which !isa, !<:, as suggested above, would be very nice.

@dhanak
Copy link

dhanak commented Sep 9, 2022

You don't have the equivalent of ∉ for other infix ops. For example isa, <:, for which !isa, !<:, as suggested above, would be very nice.

You can always define your own infix operators as aliases:

julia>  = <: # \varlefttriangle
<: (built-in function)

julia>  = !<: # \ntriangleleft
#76 (generic function with 1 method)

julia>  = isa # \vdash
isa (built-in function)

julia>  = !isa # \nvdash
#76 (generic function with 1 method)

julia> 1  Int
true

julia 1.0  Int
true

I appreciate that these would not be generally recognized, or that this is not something that easily generalizes to all infix operators, but it works if that's what you are looking for.

@stevengj
Copy link
Member

stevengj commented Mar 7, 2023

We could also just add special-case parsing for !in as a binary operator (#15353).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parser Language parsing and surface syntax
Projects
None yet
Development

No branches or pull requests

10 participants