Comment

Comment by TrianguloY

I'll throw another idea here I've been thinking from a time now.

Most languages have a while construct and a do-while.

  while(condition){block};
  do{block}while(condition);
The while is run as

    ...
  start:
    condition
    branch-if-false > end
    block
    branch-always > start
  end:
    ...
And the do-while switches the order:

    ...
  start:
    block
    condition
    branch-if-true > start
    ...
The issue with the while is that more often than not you need to do some preparations before the condition. So you need to move that to a function, or duplicate it before and inside the loop. Do-while doesn't help, since with that you can't do anything after the condition. The alternative is a while(true) with a condition in the middle.

  while(true){
    prepare;
    if(!check) break;
    process
  }
But what if there was a language construct for this? Something like

  do{prepare}while(condition){process}
Is there a language that implements this somehow? (I'm sure there is, but I know no one)

The best thing is that this construct can be optimized in assembly perfectly:

    ...
    jump-always > start
  after:
    process
  start:
    prepare
    condition
    branch-if-true > after
    ...
parent TrianguloY 1 year ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton