Sujet : Re: else ladders practice
De : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.cDate : 22. Nov 2024, 02:09:03
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vholjf$s95k$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14
User-Agent : Mozilla Thunderbird
On 10/11/2024 06:00, Waldek Hebisch wrote:
Bart <bc@freeuk.com> wrote:
...or to just always require 'else', with a dummy value if necessary?
>
Well, frequently it is easier to do bad job, than a good one.
>
I assume that you consider the simple solution the 'bad' one?
You wrote about _always_ requiring 'else' regardless if it is
needed or not. Yes, I consider this bad.
I tried the earlier C example in Rust:
fn fred(n:i32)->i32 {
if n==1 {return 10;}
if n==2 {return 20;}
}
I get this error:
Error(s):
error[E0317]: if may be missing an else clause
--> 1022687238/source.rs:5:5
|
3 | fn fred(n:i32)->i32 {
| --- expected `i32` because of this return type
4 | if n==1 {return 10;}
5 | if n==2 {return 20;}
| ^^^^^^^^^^^^^^^^^^^^ expected i32, found ()
|
= note: expected type `i32`
found type `()`
= note: `if` expressions without `else` evaluate to `()`
= help: consider adding an `else` block that evaluates to the expected type
error: aborting due to previous error
So Rust here is behaving exactly the same as my language (mine just says 'else needed').
Rust is generally a well-regarded and well-designed language. It also has clear and helpful error messages.
Presumably you would regard this as 'bad' too.
In this case the behaviour is not the easy solution, as Rust compilers are even slower and more complex than big C compilers. It is just a language choice.