5 ms·
Looks interesting. Personally I would have simplified its syntax a little bit more, if possible. For instance, the following example code main() { s
by stefanos82 4y ago
Looks interesting.
Personally I would have simplified its syntax a little bit more, if possible.
For instance, the following example code
main() {
sum := 0
for v in [5, 7, 11] index i //index is optional
{
if v == 11 { break }
sum += v
if sum > 20 { break }
print("$i: $v")
}
_OnBreak { //<-- must be immediately after loop
print("Index $i broke out of loop")
}
_OnComplete { //<-- Will not execute on break
print("Array is OK")
}
}
would have wrote it as follows:
main() {
sum := 0
for v in [5, 7, 11] index i //index is optional
{
break if v == 11
sum += v
break if sum > 20
print("$i: $v")
}
_OnBreak :: print("Index $i broke out of loop") //<-- must be immediately after loop
_OnComplete :: print("Array is OK") //<-- Will not execute on break
}
I honestly like simple, well-written code that is easy to follow.
- kleiba 4y agoDe gustibus non est disputandum - I like the original version much better than your proposal.
- levodelellis 4y agoFunny that you wrote `break if v == 11`. In a prototype I wrote 5 years ago (to learn syntax handling) I implemented this. What ended up happening is if I could do `break if ...` then I'd wanted to do expr if. Like `fn() if v == 1`. But at that point it gets confusing to read. Maybe I can try implementing this only for `break`, `continue` and `return` but it might annoy people because expressions aren't allowed. If expressions are then it's back to the problem I had in the prototype I don't know if it'll make things more clear to you but curlys aren't nessicary when you use return/break/continue. I didn't write it because I didn't want to scare people off with the lack of curlys or confuse people on the front page The `::` part looks nice. Would you want the statement to be forced on the same line? Or not like how C allows statements after `else`? As mentioned the compiler forces curly braces so it'll have to be forced to be on the same line as the on statement and I don't know if you'll find that to be a dealbreaker
- jstanley 4y ago> But at that point it gets confusing to read. What's confusing about that? Perl has that, I use it all the time.
- forgotmypw17 4y agoWhat I find confusing about it is that it does not match the execution order. Perl is my main, and I never use that feature.
- jbverschoor 4y agoIt’s way more clear and robust than a one liner if. A for loop also doesn’t match what’s being executed. It’s s actually a do/while loop
- forgotmypw17 4y agoOne-liner if statements is another pattern I stay away from as much as possible...
- jbverschoor 4y agoI usually use it to do some pre conditional checks, or to return early
- forgotmypw17 4y agoThat's fair. I still use 3-liners for that.
- levodelellis 4y agoComparing perl to a language that wants to be readable is very funny to me For me, the problem is when my expression got long or if I call a function with multiple parameters. The more complex the line is the easier it is to not notice the `if`. Also I agree with what the other person said, it doesn't match the order if you read top down left to right
- webmobdev 4y agoI don't know why people are downvoting you for sharing your suggestions. Your syntax suggestion for _OnBreak and _OnComplete does seem better for the following reason - 1. Both are related to the for loop, and the usage of :: makes them stand out and seems to gives them more context. 2. Easier and elegant to read. (But is that because the example uses a 1 line code? What would be the best way to present multiline code without confusion?) (I would have preferred if the _OnBreak and _OnComplete statements was inside the for loop as that removes any ambiguity that it is part of it and effects the for loop). However, `if v == 11 {break}` is definitely more readable than `break if v == 11`. If you are using curly braces as part of your syntax, and for blocks of code, it's better to use it with consistence. That can be confusing with `break {if v == 11}` as it breaks the syntax grammar, when you consider what break does.
- jbverschoor 4y agoMaybe do some Ruby programming and then reconsider what you think is more readable. the break, food out break out of the if block? Or somewhere else? It’s confusing
- webmobdev 4y agoThat's silly - this isn't Ruby. It's a new language still under development and thus will have its own syntax. Obviously any language will be unreadable if you don't understand its grammar (syntax). Readability is tied to the syntax of a programming language. (And Ruby is great example of this - I am used to Python and I found reading Ruby code to be very cumbersome because of its weird syntax, which I didn't bother to learn). This a C / C++ / D like language.