Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Having Tcl_WrongNumArgs return TCL_ERROR makes extension writing a little nicer

Open2c519736077ac092dbbce446759063f5f8427084Created 8 years agoUpdated 8 years ago
Descriptionwiki2018-07-04 11:50:45
In Tcl extensions, a call to Tcl_WrongNumArgs is almost invariably followed by return TCL_ERROR.
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);
}
sebreswiki2018-07-04 18:56:32

Why not so?

if (objc != 1) {
  return Tcl_WrongNumArgs(interp, 1, objv, NULL), TCL_ERROR;
}
Without any change of the interface.

stuplain2018-07-04 20:47:28
Interesting suggestion, but I don't write code like that, and neither should anyone else. :D
oehharwiki2018-07-05 06:26:19

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

sebreswiki2018-07-05 10:33:21

> 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.

stuplain2018-07-06 18:56:01
Also nice if Tcl_SetObjResult were to return TCL_OK.
Log in (anonymous is fine) to add a comment.