Sujet : Re: else ladders practice
De : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.cDate : 05. Nov 2024, 16:03:54
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vgdc4q$1ikja$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14
User-Agent : Mozilla Thunderbird
On 05/11/2024 12:42, Waldek Hebisch wrote:
Bart <bc@freeuk.com> wrote:
>
Then we disagree on what 'multi-way' select might mean. I think it means
branching, even if notionally, on one-of-N possible code paths.
OK.
The whole construct may or may not return a value. If it does, then one
of the N paths must be a default path.
You need to cover all input values. This is possible when there
is reasonably small number of possibilities. For example, switch on
char variable which covers all possible values does not need default
path. Default is needed only when number of possibilities is too
large to explicitely give all of them. And some languages allow
ranges, so that you may be able to cover all values with small
number of ranges.
What's easier to implement in a language: to have a conditional need for an 'else' branch, which is dependent on the compiler performing some arbitrarily complex levels of analysis on some arbitrarily complex set of expressions...
...or to just always require 'else', with a dummy value if necessary?
Even if you went with the first, what happens if the compiler can't guarantee that all values of a selector are covered; should it report that, or say nothing?
What happens if you do need 'else', but later change things so all bases are covered; will the compiler report it as being unnecesary, so that you remove it?
Now, C doesn't have such a feature to test out (ie. that is a construct with an optional 'else' branch, the whole of which returns a value). The nearest is function return values:
int F(int n) {
if (n==1) return 10;
if (n==2) return 20;
}
Here, neither tcc not gcc report that you might run into the end of the function. It will return garbage if called with anything other than 1 or 2.
gcc will say something with enough warning levels (reaches end of non-void function). But it will say the same here:
int F(unsigned char c) {
if (c<128) return 10;
if (c>=128) return 20;
}