Syntactic sugar: (x==2) and y=12 ; or even (x == 2) && y=12


#1

can you tell that I used perl a lot :wink: .

could

julia> (x == 2) && y=12

clash with anything? julia does not have the less binding “and” and “or” keywords afaik. I would love them, too.

I think the user meaning for the ‘&&’ construct is amply clear, but julia of course complains (correctly) about an invalid assignment location. yes, special cases are ugly and confusing and should be created only very rarely.

currently I use

julia> (x==2) && ( y=2 )

but it looks a little too similar to (x==2) && (y==2) to me. I know that special cases are not particularly desirable. ‘and’ and ‘or’ would be better.

not important, but I think convenient.


#2

just to build on this. kind of similar to:


#3

glad to see it’s still open. I agree with stefan about the ‘and’ and ‘or’ (perl) style solution. /iaw


#4

It would also be possible to make the precedence of = relative to && and || depend on whether it’s on the left or the right. We do that with some other operators and syntaxes.


#5

what scares me about context-dependent precedence is unforeseen consequences…I am not so smart as to anticipate everything that could happen in the future.

the “and” and “or” keywords seem safe, should be painless to implement, and do pretty much for the user what they are expected to do. I doubt that they would find a better use elsewhere.


#6

That discussion was already had, however, and we decided not to add and and or keywords/infix operators.


#7

I will be happy to take anything. looking at my current project, about 1 in 10-20 of the lines in my code seem to consist of a condition deciding on an assignment, though not suitable for the trinary.


#8

A good solution here could be to allow ? without :: condition ? x = 2.


#9

+1 for this. I was surpised it didn’t work when i first used it, and I think it reads much better as a sentence than &&.

Although I do also like writing x == 2 ? x = 1 : nothing since it reads easiest, kind of like where T <: Any vs. where T.