-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Make ? work without : #6823
Comments
Great! What would Y = flag ? x = 1 : 4 Mean? |
Same thing it means today? Sorry, I guess I'm not getting your point :) |
Some might think you are assigning a range. flag ? x = (1:4) |
Good point. On the other hand, is it really much worse than what is currently happening? At least I had to try out what
actually does. It just seems that one needs to know the rule for how ranges and ?: interact in any case, right? Another option could be to use ?? for this. In any case, the flat && something notation seems like a hack to me with a potential for a cleaner syntax. |
I could see keywords |
I don't know that I really understand the motivation. Do you not like the way If you're after a more readable alternative, I'd go for more verbose and standard instead of less. One suggestion a while ago was to introduce the The disadvantage here would be that it requires one more language keyword. I don't think it'd be too terrible to deprecate, but it's not a decision to be taken lightly. The same would be true for |
I think short-circuit evaluation is one of those things that is super natural to programmers but to most non-programmers it really looks weird to do control flow via that trick. I think code could be more easily understood if there was some way to do one-line if...then... semantic with a more obvious syntax than short-circuit evaluation. The |
It was a julia-users discussion thread. Also related is the |
Would |
That's a good point; I actually like the idea of allowing |
I'm kind of shocked that you like this. It is currently fine to put the colon on the next line in a ternary operator expression. While I think that's uncommon, it will potentially break code. I guess we can do that though. |
I don't believe that is true:
|
Well, what do you know. I guess I never tried that. Then this syntax is actually available. If we're going to start doing things with |
I just stumbled upon the "colon on next line" thing and wanted to report it as a bug, because I'm used to be able to format the ternary that way in C/C++ and wanted to do it in Julia too: α(t) = t < 1000 ? 0.5 - 0.46/1000* t
: t < 10000 ? 0.04 - 0.04/9000*(t-1000)
: 0 My current alternative looks slightly worse to me: α(t) = t < 1000 ? 0.5 - 0.46/1000* t :
t < 10000 ? 0.04 - 0.04/9000*(t-1000) :
0 Mind you, this is a very minor complaint. |
I would also expect it to be possible to have the |
I would also be in favor of |
+1 for |
Is there some new issue that tracks suggestions like |
If we drop the leading a then b
a else b ... both of which I think people will find perfectly clear / readable / obvious without explanation. Thread here: https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting |
I don't think that if 1 < 0
2 else 3
end or more generally when writing conditional expressions on a single line, |
@p-i-: that's basically already how I read |
Looking at the precedence table, I'm starting to think the Perl solution of having two different pairs of short-circuit operators for this is actually not so bad: |
Perhaps consider borrowing Ruby's post-fix conditionals? EDIT: Just to add...
Ruby shares these extra operators with Perl, and anecdotally they lead to far more confusion than benefit. Beginning developers will see these in Ruby and assume |
Sorry to revisit this, but I was thinking about how some of this stuff is parsed. In some contexts at least, I can make the Julia code ; which returns cond ? a : ;
cond ? : b (Perhaps we could have shorter forms like Current behavior (v0.6 and v0.7): julia> true ? 1 : ;
ERROR: syntax: unexpected ";"
julia> true ? : 1
ERROR: syntax: space not allowed after ":" used for quoting |
I would politely like to bump this discussion again since I think the use of
meaning
and
meaning
is just super confusing. I would argue that users expect to find whether the clause is negated in front of the clause. With this style, readers have to infer the negation from behind the clause. Also, normally the conditional has no side effects and this promotes having side effects in conditionals. As an extreme example, this approach would also suggest to new users that
where
since it requires fewer lines of code. |
There was a discussion about this pattern in Julia on the mailing list recently:
I very much see the need for something short and sweet in these cases (rather than a long if ... etc), but this particular syntax strikes me as pretty opaque to new users. I've also seen things like
Which again is super useful for e.g. argument error checking, but somehow to me looks like a misuse of &&.
Couldn't ? without a : work in these cases? I.e. the following syntax:
and
Essentially ? would just be a shorthand notation for an if.
The text was updated successfully, but these errors were encountered: