Crystal: The walrus operator :=

Created on 20 Oct 2019  ·  7Comments  ·  Source: crystal-lang/crystal

Python 3.8 has it: https://docs.python.org/3.8/whatsnew/3.8.html

I always wanted this syntax to make it easier to tell that you are doing an assignment instead of a comparison inside if, while, etc. I think we could have them be synonyms, for backwards compatibility, and let the formatter use one or the other depending on whether it's inside if, while, etc., or not.

What do you think?

Most helpful comment

Who really makes this mistake in the wild?

AFAIK python is adding := because = can't happen inside an expression, only inside a statement. Crystal doesn't have this issue because everything is an expression (at least supposed to be).

Such expressions are idiomatic in crystal, and most often than not the RHS or context is significant enough to tell whether we're assigning or testing equality:

if line = lines.first?
end

while message = channel.receive?
end

if Errno.value == Errno::ENOENT
end

TBH: it's not much, just a colon, yet I really doubt the benefit and be really bored if suddenly I must use :=, especially when switching between Ruby and Crystal. It's also confusing when unknown, especially if it's just an alias for readability and the formatter transforms an = to := —one more reason for me to not use the formatter.

All 7 comments

IMO if this were added, normal assignments inside an expression should be
deprecated; having both doing an identical function feels like it defeats
the purpose. Even if the formatter can switch between them, it won't stop
potential confusion or bugs from happening before you format (e.g. if the
formatter is a pre-commit hook or similar).

On Sat, Oct 19, 2019, 9:12 PM Ary Borenszweig notifications@github.com
wrote:

Python 3.8 has it: https://docs.python.org/3.8/whatsnew/3.8.html

I always wanted this syntax to make it easier to tell that you are doing
an assignment instead of a comparison inside if, while, etc. I think we
could have them be synonyms, for backwards compatibility, and let the
formatter use one or the other depending on whether it's inside if, while,
etc., or not.

What do you think?


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/crystal-lang/crystal/issues/8354?email_source=notifications&email_token=AAM4YSOYHBPOIPKMGJOLYPTQPO5AJA5CNFSM4JCSIGRKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HS64BFA,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAM4YSIUVJLO4QDV7E6CBIDQPO5AJANCNFSM4JCSIGRA
.

At first, := reminded me of Go 😸

Then reading the Python 3.8...ringed the bell 😄
I think it's a good one and might help Crystal be safer by preventing more human mistakes 👍

No doubt := is might stop some errors but I cannot deny I like the constancy of = with other languages. And I prefer the look of = in general.

Who really makes this mistake in the wild?

AFAIK python is adding := because = can't happen inside an expression, only inside a statement. Crystal doesn't have this issue because everything is an expression (at least supposed to be).

Such expressions are idiomatic in crystal, and most often than not the RHS or context is significant enough to tell whether we're assigning or testing equality:

if line = lines.first?
end

while message = channel.receive?
end

if Errno.value == Errno::ENOENT
end

TBH: it's not much, just a colon, yet I really doubt the benefit and be really bored if suddenly I must use :=, especially when switching between Ruby and Crystal. It's also confusing when unknown, especially if it's just an alias for readability and the formatter transforms an = to := —one more reason for me to not use the formatter.

Note that Python also requires such expressions to be parenthesized. Just having that would go a long way. No need for a new operator.

Having two operators do the same thing would just cause confusion.

Well, we could force using := inside other expressions. But then it's a bit tricky because you can do property x = 1 which technically happens inside an expression. Maybe force it inside if, while, etc.

But it seems nobody except me likes this change, so closing.

Was this page helpful?
0 / 5 - 0 ratings