I already agreed to try while true further up in the discussion, so we really are arguing āsemanticsā or is it syntax ? Anyway to me while true has always had the feel of a bug like a suspended anvil on a rope above just waiting for me to put a foot wrong. But I will give it the old college try - I might get to like it.
Thatās not accurate, thereās a list of keywords that end a block: catch else finally among them. (I canāt give you the source link as Iām on a phone.) Conceivably, we could add until to that list in Julia v2.
I still donāt understand the reasoning behind this, but experimenting in packages before proposing various additions to Base and the standard libraries is very common, so if you rule this possibility out from the start then you significantly lower the chances of a change like this being even considered.
Sure, you cannot extend the parser this way, but everything else is possible, particularly with macros. The best solution is to write something super useful in a nice way, package it up, and then at some point if enough people use it the proposal will speak for itself.
I guess Iāve only been following this half-heartedly but itās starting to look to me like people are suggesting things that are entirely equivalent to
while true
# stuff here
condition && break
end
but with slightly different syntax. I donāt understand why this is desirable, is the above really so bad? I appreciate the point that alternate syntax is sometimes desirable, but this seems like a case where the existing syntax is simple, elegant and general.
Code I write goes into production. Iām pretty conservative about loading packages - they are loaded for functional need not syntactic sugar, it is not good practice (unless the syntactic sugar becomes essentially functional). Something like this is a pretty fundamental language change that should not really be done on the package level.
Itās the kind of thing Iād test out in a script for different use cases and leave it at that.
@dataDiver, each language has its own aesthetic, Julia is in my opinion heavily influenced by Common Lisp, and in Common Lisp macros are everywhere and you just have to like it. Keeping the base language small is a choice that makes for good extensibility.
I think a package that specifically contains a lot of common idiom macros is exacty whatās needed here. Like some sort of UtilityMacros.jl
The alternative is to pour everything into the base language like Perl did, and I specifically find Julia exciting because it isnāt like Perl
I have to agree with @dlakelan, I think one of the whole points of Juliaās design is the goal of first class status for user code relative to base Julia. Macros seems like a key part of that and this situation seems like an obvious place for a macro for those who really want a do while loop.
This is pretty much what Stefan said, but just to spell it out, I find that a surprising number of times I actually want the termination condition somewhere in the middle of the loop. Hereās a random example from Base that I found immediately:
while true
n1 = read(f, Int32)
totbytes -= 4
n1 == 0 && break
skip(f, n1)
totbytes -= n1
end
So Iām not necessarily 100% against a do-while loop, but it seems like a lot of syntax to spend on a somewhat-arbitrary special case. In fact it doesnāt seem so crazy to me to entirely replace while with repeat ... end, equivalent to a while true loop. Then no condition position gets to be special. while cond is just really traditional.
Iāll also add that, given the syntax we have today, writing code twice (to do one iteration outside the loop) is more of a bug factory than while true. Itās all too easy to forget to update one of the copies of the code.
I support the idea that while true and break is not a great idea, as it does not follow the good programming practices nor clean coding.
That said, Julia main goal is to be fast. Thinking about C language for example, using a lot of breaks on long loops may lead to a slower IPC. Julia does a great job optimizing code, so we have less issues about performance here.
So in the end, we need to think about the main focus of the language: performance or clean coding.
Funny you should say this. I searched my edition of the Robert C Martin Clean Code Collection, and found multiple examples for recommended code, eg in Appendix A (Client Based Locking):
IntegerIterator iterator = new IntegerIterator();
while (true) {
int nextValue;
synchronized (iterator) {
if (!iterator.hasNext())
break;
nextValue = iterator.next();
}
doSometingWith(nextValue);
}
(sorry if the indentation was mangled with copy-paste).
Maybe we should remove all loops and replace them by macros, this way while is not privileged over @dountil and nobody can complain about their missing loop construct. Also, I think that is what the compiler does anyways
macro repeat(body)
quote
@label start
$body
@goto start
@label break
end |> esc
end
macro while(cond, body)
quote
@label start
if $cond
$body
@goto start
end
@label break
end |> esc
end
macro dountil(body, cond)
...
This code is for illustration purposes only and does not work because it uses keywords,
I see that on some cases it can be useful (or the only option available), but it doesnāt make it easy to read. Just imagine a larger loop or multiple breaks.