Range/interval syntax in `if` statements

Has it been mentioned anywhere why the ..< and ..= aren’t available in if statements like they are in switches? Has it been considered?
I feel like it’s, while not critical, a very natural feature and would simplify code like alpha_ranges[p] <= c && c <= alpha_ranges[p+1] (from letter.odin)

You can use the range operators in a switch statement.

I mention it in the post, yes. My question is about ifs

After a quick search, it looks like a similar proposal was suggested not too long ago. This discussion was promptly closed by the language maintainer Ginger Bill.

Personally, I don’t mind verbose range checking in an if statement, but you could get closer to your proposed aesthetic with a procedure call. Something like this:

import "base:intrinsics"

value_in_range :: proc (value, low, high: $T) -> bool 
	where intrinsics.type_is_numeric(T) {
	return (value <= high && value >= low) ? true : false
}

This would enable you to get kinda close to the spirit of your proposal.

if value_in_range(c, alpha_ranges[p], alpha_ranges[p+1]) {
	// ...
}

1 Like

Sadly for you, this is not going to be allowed.

The main reason it is allowed in a switch case is because there is no ambiguity to what it means and does not require any extra semantics.

In a switch statement, it doesn’t really make any sense either. What would the syntax be? And before you suggest if x in a..<b, that then implies that ranges are not values in themselves and not just syntactic sugar in specific cases. This is where things get complicated and I don’t want to go down that rabbit hole.

And in the case of a switch case, each value of that range might actually just be an entry in the jump table.

2 Likes