Hello,
L accepts that we use ==
to compare structs, but refuses that we use !=
and insists that we use eq()
instead (it should rather suggest ne()
, by the way).
This doesn’t seem very coherent.
Example: if I uncomment the last part, I get:
./bug_struct_comparison.l:56: L Error: only eq() allowed on non-scalar types
Otherwise I get all the expected results (==
really compares the structures fields).
#!/usr/local/bin/L
struct mystruct {
int i;
string sa[];
};
void ok() { puts("pass"); }
void ko() { puts("FAIL"); }
void main() {
struct mystruct A1 = {5, {"abcdef", "ghij"}};
struct mystruct A2 = {5, {"abcdef", "ghij"}};
struct mystruct A3;
struct mystruct B = {5, {"abcdef", "ghik"}};
A3.i = 3;
push(&A3.sa, "abc");
push(&A3.sa, "ghij");
A3.sa[0] .= "def";
A3.i++;
A3.i++;
if(A1==A2) {
ok();
} else {
ko();
}
if(A1==A3) {
ok();
} else {
ko();
}
if(A1==B) {
ko();
} else {
ok();
}
if(!(A1==A2)) {
ko();
} else {
ok();
}
if(!(A1==A3)) {
ko();
} else {
ok();
}
if(!(A1==B)) {
ok();
} else {
ko();
}
/*
if(A1!=B) {
ok();
} else {
ko();
}
*/
}
If I look at tcl/generic/Lcompile.c
, line 3633:
if (!isscalar(expr->a) && (expr->op != L_OP_EQUALEQUAL)) {
L_errf(expr, "only eq() allowed on non-scalar types");
return (0);
}
So, there is an exception for L_OP_EQUALEQUAL
. Why can’t L_OP_NOTEQUAL
enjoy the same treatment?
Other L_LOP_xxx
rely on ordered comparisons (greater, less), so it sounds normal that they cannot be applied to structures. But there should not a difference in processing between ==
and !=
: if it makes sense to make one and it can be done, the same goes for the other one.
What do you think about this?