Proposal: x == 2 should not be a valid statement


(Amir Michail) #1

It’s too easy to accidentally write “x==2” instead of “x=2”.

I don’t think something like “x==2” should be a valid statement.


(Jonathan Tang) #2

I'd love to see this become a compiler warning. Unsure about making it an
error; how would it affect implicit return from lambdas?

···

On Thu, Feb 11, 2016 at 3:32 PM, Amir Michail via swift-evolution < swift-evolution@swift.org> wrote:

It’s too easy to accidentally write “x==2” instead of “x=2”.

I don’t think something like “x==2” should be a valid statement.


(Joe Groff) #3

This should be covered by the standard library making `==` @warn_unused_result and/or the language making @warn_unused_result the default behavior for all declarations.

-Joe

···

On Feb 11, 2016, at 3:32 PM, Amir Michail via swift-evolution <swift-evolution@swift.org> wrote:

It’s too easy to accidentally write “x==2” instead of “x=2”.

I don’t think something like “x==2” should be a valid statement.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Jessy) #4

I don’t think something like “x==2” should be a valid statement.

I agree, but I think that is best dealt with by removing == from Swift entirely. == is only useful in languages where a value is returned from an assignment operation.


(Haravikk) #5

I agree with this. I have kind of wondered why @warn_unused_result isn’t actually the default, as I forget to add it nine times out of ten, when in reality almost all of my methods that return something should have it. Is there an existing discussion for switching the behaviour?

But yeah, I think warning of unused comparison results should solve this issue well enough, just like misusing a function/method call.

···

On 11 Feb 2016, at 23:52, Joe Groff via swift-evolution <swift-evolution@swift.org> wrote:

This should be covered by the standard library making `==` @warn_unused_result and/or the language making @warn_unused_result the default behavior for all declarations.


(Radek Pietruszewski) #6

I agree an expression like `x==2`, not being passed on or assigned to anything, should generate a warning and a helpful fixit. No reason to make it an error.

Implicit return doesn’t change this AFAICT.

— Radek

···

On 12 Feb 2016, at 00:41, Jonathan Tang via swift-evolution <swift-evolution@swift.org> wrote:

On Thu, Feb 11, 2016 at 3:32 PM, Amir Michail via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
It’s too easy to accidentally write “x==2” instead of “x=2”.

I don’t think something like “x==2” should be a valid statement.

I'd love to see this become a compiler warning. Unsure about making it an error; how would it affect implicit return from lambdas?
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Allen Ding) #7

-1 from me.

This is a fundamental change to an operator that is one of the most
frequently used in the language. IMO, any alternative has to offer the same
ease of use in addition to any theorized benefits.

FWIW, I understand the potential for confusion and accidental typos, but
with warnings from tooling (including in various other languages), I can't
recall an instance of having made this particular in the past few years, so
it isn't that big of a deal to me.

- Allen

···

On Fri, Feb 12, 2016 at 7:32 AM, Amir Michail via swift-evolution < swift-evolution@swift.org> wrote:

It’s too easy to accidentally write “x==2” instead of “x=2”.

I don’t think something like “x==2” should be a valid statement.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(John Randolph) #8

What do you propose as an alternative operator for testing equivalence?

-jcr

···

On Feb 11, 2016, at 3:37 PM, Jessy Catterwaul via swift-evolution <swift-evolution@swift.org> wrote:

I don’t think something like “x==2” should be a valid statement.

I agree, but I think that is best dealt with by removing == from Swift entirely. == is only useful in languages where a value is returned from an assignment operation.


(Joe Groff) #9

Yeah, this is being discussed: https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151221/003879.html

-Joe

···

On Feb 11, 2016, at 3:57 PM, Haravikk <swift-evolution@haravikk.me> wrote:

On 11 Feb 2016, at 23:52, Joe Groff via swift-evolution <swift-evolution@swift.org> wrote:

This should be covered by the standard library making `==` @warn_unused_result and/or the language making @warn_unused_result the default behavior for all declarations.

I agree with this. I have kind of wondered why @warn_unused_result isn’t actually the default, as I forget to add it nine times out of ten, when in reality almost all of my methods that return something should have it. Is there an existing discussion for switching the behaviour?


(Jessy) #10

What do you propose as an alternative operator for testing equivalence?

···

=

http://catterwaul.com/equality-operator/

Jonathan Tang’s brings this up:
{x == 2}

You’d need to use the return keyword. Oh well; I think it reads better.
{return x = 2}


#11

A quick test shows that == is already @warn_unused_result
This is a inside vs outside playground issue,
playground hides all these @warn_unused_result (after all the result is display thus used)

Dany

···

Le 11 févr. 2016 à 18:57, Haravikk via swift-evolution <swift-evolution@swift.org> a écrit :

On 11 Feb 2016, at 23:52, Joe Groff via swift-evolution <swift-evolution@swift.org> wrote:

This should be covered by the standard library making `==` @warn_unused_result and/or the language making @warn_unused_result the default behavior for all declarations.

I agree with this. I have kind of wondered why @warn_unused_result isn’t actually the default, as I forget to add it nine times out of ten, when in reality almost all of my methods that return something should have it. Is there an existing discussion for switching the behaviour?

But yeah, I think warning of unused comparison results should solve this issue well enough, just like misusing a function/method call.


(Myles La Verne Schultz) #12

Agreed, -1, ‘==‘ is a very common idiom in Swift.

···

On Feb 11, 2016, at 8:54 PM, Allen Ding via swift-evolution <swift-evolution@swift.org> wrote:

-1 from me.

This is a fundamental change to an operator that is one of the most frequently used in the language. IMO, any alternative has to offer the same ease of use in addition to any theorized benefits.

FWIW, I understand the potential for confusion and accidental typos, but with warnings from tooling (including in various other languages), I can't recall an instance of having made this particular in the past few years, so it isn't that big of a deal to me.

- Allen

On Fri, Feb 12, 2016 at 7:32 AM, Amir Michail via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
It’s too easy to accidentally write “x==2” instead of “x=2”.

I don’t think something like “x==2” should be a valid statement.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Vanderlei Martinelli) #13

Strong -1 for removing or change `==`.

I warned about something like this when talking about remotion of `++` and
`--` before.

It seems that the whole concern is going toward preventing all the ways
that a developer, well ... Develop.

We are not talking about the future of the Pages app, but now imagine that
it prevents you from writing the text the way you want, because it is safer
or modern and this is "better for you".

Maybe I'm wrong understanding, but the future of the Swift language
involves it be used by programmers, is not it? I imagine that programmers
know what `==` and `=` are and do not confuse them with each other or
something else.

Good programmers make a good language, but there is no good language in the
world that can make someone become a good programmer if he/she is not.

-Van

···

On Fri, Feb 12, 2016 at 12:12 AM, Myles La Verne Schultz via swift-evolution <swift-evolution@swift.org> wrote:

Agreed, -1, ‘==‘ is a very common idiom in Swift.

On Feb 11, 2016, at 8:54 PM, Allen Ding via swift-evolution < > swift-evolution@swift.org> wrote:

-1 from me.

This is a fundamental change to an operator that is one of the most
frequently used in the language. IMO, any alternative has to offer the same
ease of use in addition to any theorized benefits.

FWIW, I understand the potential for confusion and accidental typos, but
with warnings from tooling (including in various other languages), I can't
recall an instance of having made this particular in the past few years, so
it isn't that big of a deal to me.

- Allen

On Fri, Feb 12, 2016 at 7:32 AM, Amir Michail via swift-evolution < > swift-evolution@swift.org> wrote:

It’s too easy to accidentally write “x==2” instead of “x=2”.

I don’t think something like “x==2” should be a valid statement.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(John Randolph) #14

So, == gets replaced with six characters, plus a special case of “=“?

-1 from me. Don’t like this idea at all.

-jcr

···

On Feb 11, 2016, at 3:50 PM, Jessy Catterwaul via swift-evolution <swift-evolution@swift.org> wrote:

What do you propose as an alternative operator for testing equivalence?

=

http://catterwaul.com/equality-operator/

Jonathan Tang’s brings this up:
{x == 2}

You’d need to use the return keyword. Oh well; I think it reads better.
{return x = 2}


(Jessy) #15

So, == gets replaced with six characters, plus a special case of “=“?

-1 from me. Don’t like this idea at all.

Fair enough. How about “is”?

{x is 2}


(Radek Pietruszewski) #16

I definitely don’t like the idea of = meaning two very different things, whether or not it’s unambiguous to the compiler.

Why break decades-long programming convention for this?

— Radek

···

On 12 Feb 2016, at 00:50, Jessy Catterwaul via swift-evolution <swift-evolution@swift.org> wrote:

What do you propose as an alternative operator for testing equivalence?

=

http://catterwaul.com/equality-operator/

Jonathan Tang’s brings this up:
{x == 2}

You’d need to use the return keyword. Oh well; I think it reads better.
{return x = 2}
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Jean-Denis Muys) #17

Strong -1

Swift is in the C-family of languages, syntax-wise. == as the comparison operator is as much rooted there as are braces.

All these are questionable on similar grounds: ==, += -= and so on, braces as block delimiters, even the single = sign

It’s too easy to accidentally write “x==2” instead of “x=2”.

And I can also argue that +, - *, / are also bad, because 'It’s too easy to accidentally write “x+2” instead of “x-2”.’ and therefore we should replace them with ADD, SUBTRACT, MULTIPLY, DIVIDE

or something

What is the reason for Swift to use C-like syntactic constructs? I suppose it’s familiarity. Granted, familiarity might not be the strongest argument. Yet it counts.

== is familiar, unambiguous, and the compiler can catch most cases of mistyping.

JD

···

On 12 Feb 2016, at 00:50, Jessy Catterwaul via swift-evolution <swift-evolution@swift.org> wrote:

What do you propose as an alternative operator for testing equivalence?

=

http://catterwaul.com/equality-operator/

Jonathan Tang’s brings this up:
{x == 2}

You’d need to use the return keyword. Oh well; I think it reads better.
{return x = 2}
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(John Randolph) #18

Besides the fact that it’s a decades-long convention, there’s also the reason that it was adopted in the first place: it’s short, and easy to remember.

-jcr

···

On Feb 11, 2016, at 3:54 PM, Radosław Pietruszewski <radexpl@gmail.com> wrote:

Why break decades-long programming convention for this?


(John Randolph) #19

I’d say that “is” is more appropriate for replacing the identity operator “===“. If we did that though, we’d have to also have “isn’t” or “!is” in our code.

“==“ for equivalence isn’t broken. It doesn’t need fixing.

-jcr

···

On Feb 11, 2016, at 3:59 PM, Jessy Catterwaul <mr.jessy@gmail.com> wrote:

So, == gets replaced with six characters, plus a special case of “=“?

-1 from me. Don’t like this idea at all.

Fair enough. How about “is”?

{x is 2}


(Jessy) #20

Why break decades-long programming convention for this?

Some people using Swift aren't even a decade long.

== exists because old languages returned a value from assignment. Not because it is good.