Julia: Feature Request: Differentiate Between `in` And `=`

Created on 10 Oct 2020  路  4Comments  路  Source: JuliaLang/julia

As far as I understand, Julia interprets = and in synonymously in list comprehension. Also, some types, like numbers, are iterable where they are interpreted as singletons, so [x*x for x in 2] gives 4. This feels a bit too much overloaded for me, why not make in assume that some array-like type is given and = T be treated as in [T]? So that e.g. [x*x for x in [2]] and [x*x for x = 2] both return 4 but [x*x for x in 2] returns an error?

My personal history is that I had the hunch that Julia maybe treats = T as in [T] in list comprehension, so I tried it with a number and it worked, but not for the reason that I assumed, so later I had = CartesianIndex(i, j) somewhere in my code and got an error because cartesian indices are not iterable. Took me some while to figure out what is going on.

Most helpful comment

If anything I would just deprecate = here.

All 4 comments

Irrelevant of the potential pros and cons, the change would be huge and so you might want to read this post by Stefan: https://discourse.julialang.org/t/psa-julia-is-not-at-that-stage-of-development-anymore/44872

I don't think giving them different meanings is a good idea, especially because for i = ... is also used in MatLab. I do think, it might make sense to differentiate the two in the AST at least, so macros could at least differentiate them, but that would probably have to be a 2.0 thing. It might also be more of a hassle than what it's actually worth though.

If anything I would just deprecate = here.

Thanks for the comments, they make sense and I learned something. Sorry for not reading Stefan's post in beforehand.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

i-apellaniz picture i-apellaniz  路  3Comments

musm picture musm  路  3Comments

omus picture omus  路  3Comments

wilburtownsend picture wilburtownsend  路  3Comments

StefanKarpinski picture StefanKarpinski  路  3Comments