Hi, I recently started learning Nim. Nim is an excellent language, but I worry about less support of types. Even primitive types are not well supported by basic functions.
For example, I can't compare two different types of integers.
Nim
var
x = 100'u64
y = 100'i32
if x == y:
echo "x eq y"
This code fails because it's comparing two different integer types. However, in C,
C
int main(){
unsigned long long x = 100;
int y = 100;
if (x == y){
printf("x eq y");
}
}
This code works as expected.
Why does Nim not support different types of comparison?
char a = 0x80;
char b = a << 1;
printf("%s equal\n",b == a << 1 ? "" : "not");
What will that print?
integer promotion is evil.
This code works as expected.
No this is one of large source of bugs in C codebases, and why everyone is promoting safer languages.
The whole point of a type system is avoiding mixing apples and oranges and ducks unless you explicitly ask for it.
There is also a problem in @shirleyquirk's example of whether unmodified char is signed or unsigned: https://stackoverflow.com/questions/2054939/is-char-signed-or-unsigned-by-default
Basically char a could be either +128 or -128 before it is promoted to a C int. So, a << 1 can be either + or -256. Both those values happen in this case to get implicitly truncated to 0 by being stored in char b.
It's subjective what is most confusing about all this, of course. ;-)