top | item 34958658

(no title)

dan00 | 3 years ago

I think the naming of the ‘else’ branch in the loop could be more telling, like using the name ‘finally’ or ‘finish’.

discuss

order

Someone|3 years ago

I agree, but ‘finally’ or ‘finish’ IMO aren’t good choice because that code doesn’t always execute.

I think I would go for something expressing ‘default’, but would first look at existing code to see how common this is, and if I decided I wanted this feature, look hard for alternative syntax.

  const match: ?usize = for (text, 0..) |x, idx| {
     if (x == needle) break idx;
  }
could return an optional int, for example. If so, you would get a ‘null’ for free, and if you didn’t want a null, you could tack on a .getOrElse(NOT_FOUND).

I guess they picked this because Python has it, too. https://docs.python.org/3/tutorial/controlflow.html#break-an...:

“Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the iterable (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.”

masklinn|3 years ago

`finally` hints at a very different behaviour because in most languages' context a finally clause is executed whether an exception is raised or not.