Closed lehenbauer closed 13 years ago
This is cool, I've run into this before but didn't go 'you know, that should be possible', but you're right, it should.
I'm thinking this should maybe not be the default, but it could be a ctable option?
Let me think about whether there's any possible breakage from just doing it, rather than making it an option.
I don't mind if it's an option, at all, but if it is an option it needs to flow naturally. I don't want to have to specify "-leadingdash 1" on "set" or "store", although I'm not hard over on it.
As for search, yeah, if search results were to have a leading dash in field names then it would have to be an option. Also the array_get and array_get_with_nulls methods would need a switch, if they were to support it.
It would be real natural to write
$table set $key -field value -field value -field value
We might make it that if the first key has a leading dash, all keys must have a leading dash for that single statement. That'd catch anyone screwing up the key-value pairs.
Oh yeh, it should be a cosmetic filter on set/store, just to allow iTcl/tk style method option assignments. The actual field names wouldn't have the dashes.
What about a different method, perhaps "$table option_set ..."?
I was thinking of making it a class-level (meta-table) option, but we don't seem to have that set up as a general case. The other two possibilities are: it could be an instance option set on the table when it's instantiated, or it could be an option on the method (I agree, that's ugly) or a different method.
OK, I started working on this. Technically, it's easy to implement with a small chunk of code in command-body.c-subst, eg:
if(optIndex == OPT_SET_OPT) {
int prefix_state = -1; // 1 if seen '-', 0 if seen without '-'
for (i = 0; i < listObjc; i+= 2) {
char *option = Tcl_GetString (objv[i]);
if(*option == '-') {
if(prefix_state == 0) {
Tcl_AppendResult (interp, "Can't mix prefixed and unprefixed field names", (char *)NULL);
return TCL_ERROR;
}
prefix_state = 1;
objv[i] = Tcl_NewStringObj(option+1, -1);
} else {
if(prefix_state == 1) {
Tcl_AppendResult (interp, "Can't mix prefixed and unprefixed field names", (char *)NULL);
return TCL_ERROR;
}
prefix_state = 0;
}
}
}
(with appropriate additions to the list of methods and matching enums)
Problem is with store what to do about "$table opt_store -nocomplain". "nocomplain" is a legal field name.
My call would be: opt_store and opt_set don't take qualifiers, and always act like "-nocomplain", ignoring unknown fields.
In the "set" and "store" methods, if the fieldname contains a leading dash, simply ignore the leading dash. This allows us to use field-value pairs as Itcl and Tk do.
Might want to even make a switch to get field names with a leading dash within search, I don't know.