Having Tcl_WrongNumArgs return TCL_ERROR makes extension writing a little nicer
If Tcl_WrongNumArgs, which currently has a void return, were to instead return TCL_ERROR then extension writing would be a little bit nicer.
Currently:
if (objc != 1) {
Tcl_WrongNumArgs(interp, 1, objv, NULL);
return TCL_ERROR;
}
Nicer:
if (objc != 1) {
return Tcl_WrongNumArgs(interp, 1, objv, NULL);
}
Why not so?
Without any change of the interface.
if (objc != 1) {
return Tcl_WrongNumArgs(interp, 1, objv, NULL), TCL_ERROR;
}
Interesting suggestion, but I don't write code like that, and neither should anyone else. :D
Hi,
thank you for the proposition which looks beautiful at a first glance.
IMHO any function should return the status of itself.
There might be a lot of troubble inside Tcl_WrongNumArgs which may be reported:
- Invalid interpreter
- Invalid objv
- Position number out of range
I would prefer, that the function returns an error instead of returnin a convenience value.
If you prefer, you may also define a macro or an inline proc to beautify your code. Or use the solution by Sergey.
Harald
> neither should anyone else. :D
Why not?...
Explicit looks always better as implicit... Despite the function would not return anything else as TCL_ERROR, so IMHO no return code expected also. So as regards the readability of the source code.
At first glance, the one reason speak for the implicit return code inside Tcl_WrongNumArgs - the minimization of resulting assembler code (if it's the last function in the simple stack block, modern compiler will replace "call Tcl_WrongNumArgs" with "jmp Tcl_WrongNumArgs")...
So:
test(Tcl_Interp*, int, Tcl_Obj*):
sub rsp, 8
xor ecx, ecx
call Tcl_WrongNumArgs
xor eax, eax
add rsp, 8
ret
resulting to:
test(Tcl_Interp*, int, Tcl_Obj*):
xor ecx, ecx
jmp Tcl_WrongNumArgs;
But only if nothing else is to do (free stack, decrement references, etc).
Otherwise it may be bad, because steals a register (eax) for return code from function (because the compiler does not know that it's always the same return code).
And as Harald correct said, you could simply write your own macro resp. inliner.
Also nice if Tcl_SetObjResult were to return TCL_OK.