Hello world. I've imagined a new way to write if-else statements : Instead of :
if a == 0 :
#whatever
else if a <= 4 :
#whatever
else if a <= 8 :
#whatever
else :
#whatever
We could write:
if :
a == 0 : #whatever
a <= 4 : #whatever
a <= 8 : #whatever
: #whatever
How do you view my idea?You suggesion is pretty much just a case statement.
case a:
of 0: #whatever
of 1..4: #whatever
of 5..8: #whatever
else: discard
Although if you really want that behaviour you proposed you could make your own macro that takes each condition and puts it in a if/elif branch.Definitely not. Adding syntax is very expensive and the payoff is miniscule and the syntax of choice is arguable.
The current syntax for if and case are all right.
Do you think that that macro can be added to lib/pure/sugar.nim ?
Did you miss the first reply (by @ElegantBeef), where he shows you that you basically "invented" what is already available, and that is case statement?
Note that nimpretty won't preserve the spacing, so I never use it and consider it misnamed.
Well your spacing is ugly so nimpretty's name fits. ;-)
Did you miss that it isn't a case statement, and that @ElegantBeef's case statement does something completely different?
Yep :ashamed: :)
I don't believe that anyone actually thinks that my code alignment is ugly. In any case, it makes the code easier to read and less error prone, although one can certainly go overboard.
https://shkspr.mobi/blog/2014/11/why-i-vertically-align-my-code-and-you-should-too/ https://medium.com/@tylerneylon/vertical-code-alignment-9635bd2ee08c https://softwareengineering.stackexchange.com/questions/30029/vertical-alignment-yea-or-nay
Well in my defence my case statement was based off their previous code, which I believe to have been edited since.
That's a valid defense. It's too bad that the edit history isn't accessible so that such confusion can be avoided. Or edits to messages that have been responded to could be barred. Lacking that, people making edits that invalidate responses really ought to include the original version.
I really do find it ugly. At least write it this way, whitespace before a colon is uncommon in written English.
if a(0): # whatever
elif a(4): # whatever
elif a(8): # whatever
else: # whatever
No opinion on the whitespace w.r.t colon, but I've written stuff in the past to keep the conditions aligned like this:
if false: discard
elif 3==4: echo "Something wrong"
elif 5==6: echo "Somethine else wrong"
else : echo "Something right"
which lets me reshuffle the "elif" lines and doesn't give the first any special role. (And note, I've placed the colon in the else line here to make sure everyone dislikes it!)
Yes, but that's nimpretty's job! ;-)
For sections of code you can disable nimpretty:
#!nimpretty off
if a(0): # whatever
elif a(4): # whatever
elif a(8): # whatever
else: # whatever
#!nimpretty on