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

Proposal: is not syntactic sugar #1338

Closed
TheUnlocked opened this issue Feb 25, 2018 · 24 comments
Closed

Proposal: is not syntactic sugar #1338

TheUnlocked opened this issue Feb 25, 2018 · 24 comments

Comments

@TheUnlocked
Copy link

I get that this is somewhat frivolous, but I think that

if (foo is not Bar){
     // Do something
}

is cleaner than

if (!(foo is Bar)){
     // Do something
}

I can't really think of any downsides to doing this, other than that it would take any amount development time for a relatively inconsequential syntax change. There are upsides however:

  1. It's more English-like.
  2. It doesn't require surrounding anything in parentheses, slightly reducing the amount of time you need to put into negating an is expression.
  3. In larger boolean expressions, it may decrease parenthesis clutter.

An alternative to is not could be is! or !is, though those could be somewhat confusing.

@TheUnlocked TheUnlocked changed the title is not syntactic sugar Proposal: is not syntactic sugar Feb 25, 2018
@HaloFour
Copy link
Contributor

Related to #882 and #568. This particular syntax has been suggested in some comments. However, I think that it would have to somehow work alongside pattern matching in order to fit with the is operator.

@MkazemAkhgary
Copy link

public class not {}

if(obj is not not)

I can't really think of any downsides to doing this

1- obj is not of type not

2- obj is of type not and variable not is declared with pattern matching

@Unknown6656
Copy link
Contributor

Unknown6656 commented Feb 25, 2018

@MkazemAkhgary : In this case one would have to provide either an alias or the parent namespace name, e.g.:

namespace kek
{
    public class not {}
    

    public static class Program
    {
        public static int main(string[] argv)
        {
            ...
            if(obj is not kek.not)
            ...
        }
    }
}

I do not think that that would be a bad solution, as one does rarely name a class not:
It is against the standard C# naming convention and it is pretty non-descriptive (except e.g. when it represents a not-opcode in a compiler or something like that).

@lachbaer
Copy link
Contributor

lachbaer commented Feb 25, 2018

This came up already so often, but somehow I got the feeling that it is the word not that hinders the construct to make progress in its career 😉 . I'll throw in leaving the C-ish ! char for the meaning of not:

if (foo is ! Bar) { ... }

(this acutally looks a bit spanish ¡s! 😁 )

@Neme12
Copy link

Neme12 commented Feb 25, 2018

I'd be for this if not is actually a recursive pattern (as opposed to an extension of is), so it would be possible to use case not Bar b for example.

@MkazemAkhgary
Copy link

I don't think not is really helping here. it just gives vague meaning. case not is definitely vague, if there are 2 case not then which one should be chosen if given argument is neither of two cases?

you cant have more than one case not and there is already default case for this purpose.

english language is not a programming language.

@gafter
Copy link
Member

gafter commented Feb 26, 2018

Order is significant in a switch statement. The first matching case is taken. If none match then a default label, if present, is taken.

@Richiban
Copy link

Having not as a way of inverting a pattern could prove to be incredibly useful, especially in recursive situations. E.g.

switch(point)
{
    case Point2D(not 0, not 0): ...
    case not Point3D(_, _, _): ...
    default: ...
}

@Neme12
Copy link

Neme12 commented Feb 26, 2018

@Richiban yes it would be useful especially in recursive patterns... but this of course begs the question: what if you want to match if the value is > 0? or any other comparison? would this feature be a dead end?

@Neme12
Copy link

Neme12 commented Feb 26, 2018

but of course inverting a pattern is a much more general concept than inequality...

i'm just a little worried that in this particular case (of Point(not 0, not 0) it might not be so much useful after all/ it would have a very narrow use case because:
a) it would only work if you want inequality
b) you can't extract the value this way

that said, i would still prefer this as a general pattern rather than some syntactic sugar around is (in fact i don't think i want such a feature at all, this doesn't feel right to me to make another version of is just to have a nice word in there and make it read well in 1 very specific case, and a make new contextual keyword for that)

@deniszykov
Copy link

There is syntax for this:

if (foo is Bar == false) {
     // Do something
}

@Neme12
Copy link

Neme12 commented Feb 28, 2018

how about foo is Bar is false 😄 (also existing syntax)

@MkazemAkhgary
Copy link

how about

dear computer, see if foo is bar, then do this.

yes, a little exaggeration, just to show you my feelings about 1-It's more English-like.

@yaakov-h
Copy link
Member

yaakov-h commented Mar 1, 2018

This is csharplang, not applescriptlang 😄

@TruePluto
Copy link

@MkazemAkhgary How about using "isnot" instead of "is not"

public class not {}

if(obj isnot not)

@jnm2
Copy link
Contributor

jnm2 commented Mar 1, 2018

I like is not, not as much isnot, and I still like the informality of isnt. The potential standalone not pattern seems the most interesting though.

@gafter
Copy link
Member

gafter commented Mar 1, 2018

Added #1350 to champion this idea as a pattern form, along with some previous suggestions from @alrz.

@CyrusNajmabadi
Copy link
Member

If it starts with i, it's an interface, and thus should be written ISnot. I.e. the set of functionality in things that are snot-like.

@gafter
Copy link
Member

gafter commented Mar 2, 2018

@CyrusNajmabadi Since @jnm2 likes informality, I think we should go with aint.

@jnm2
Copy link
Contributor

jnm2 commented Mar 2, 2018

@CyrusNajmabadi If my nose was running money...

@sharwell
Copy link
Member

Duplicate of #27

@gafter
Copy link
Member

gafter commented Apr 21, 2018

Already championed as part of #1350

@AustinBryan
Copy link

Personally, I think this is much cleaner:

if (foo aint bar)
{

}

😜

@TheUnlocked
Copy link
Author

Closing because this was championed a while ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests