From: <apn...@ya...> - 2025-01-30 16:08:44
|
(Probably a question for Donal, but sending to the list in case folks have ideas / opinions for or against) The [const] command implementation in Tcl 9 essentially bars writes to specified variables. How hard would it be to loosen this constraint mechanism to instead only bar certain writes, for example, writing values that do not match a specified type? So, in Tcl 9 const x 42 set x 43 <- disallowed Similarly, Int32 x 42 Set x 43 <- allowed Set x abc <- disallowed Etc. A generalized constraint mechanism will introduce many complexities but just type check does not seem hard to implement. The motivation by the way is to simplify interfaces to external API's in other languages and data exchange by not having to carry typing tags. Any comments on (a) feasibility, and (b) implications, if any, for Tcl semantics ? /Ashok |
From: Donal F. <don...@ma...> - 2025-01-30 17:11:00
|
That's quite complicated as it stands; constant-ness is a single bit flag right now that is tested for before analysing the variable in any depth (i.e., before searching for traces) so it's pretty cheap. Binding a type is more complex; the current simplest mechanism for it is what we have for Tcl_LinkVar() and that involves traces and a way to unwind rejected writes as traces run after the fact. A type check would probably be better done through a pre-write authorization callback so that there's no need to unwind failures; we don't have such a thing right now so this would be new code to hang on the Tcl_Var ekeko. In the simplest cases we'd be able to limit that to a "conforms to type" check but it's only easy to write such things for basic types. I'd not expect a speed boost from doing this, not without some sort of smart compilation to remove the need to check the type in the first place (and possibly unbox the value, for simple-enough types such as the one in your example). Obviously, no such compiler exists at the moment as it would be dependent on the model changes sketched in my previous paragraph. In general, type constraints are complex because types are complex, as one inevitably wants to start describing types of compound objects like lists and dicts, which are a yawning abyss of tricky cases. It's a proper can of worms! (As evidence, I cite the enormous complexity introduced by typing in Python, not made easier by their different approach to mutability.) Basically, we can add type constraints pretty easily (one more callback type to run as part of a variable write prior to the variable being written) but defining the space of type constraints that can be enforced at that point is VERY HARD! And constants don't use that mechanism. They just deny all writes. Donal. ________________________________ From: apnmbx-public--- via Tcl-Core <tcl...@li...> Sent: Thursday, January 30, 2025 16:08 To: tcl...@li... <tcl...@li...> Subject: [TCLCORE] On type checking variables (Probably a question for Donal, but sending to the list in case folks have ideas / opinions for or against) The [const] command implementation in Tcl 9 essentially bars writes to specified variables. How hard would it be to loosen this constraint mechanism to instead only bar certain writes, for example, writing values that do not match a specified type? So, in Tcl 9 const x 42 set x 43 <- disallowed Similarly, Int32 x 42 Set x 43 <- allowed Set x abc <- disallowed Etc. A generalized constraint mechanism will introduce many complexities but just type check does not seem hard to implement. The motivation by the way is to simplify interfaces to external API’s in other languages and data exchange by not having to carry typing tags. Any comments on (a) feasibility, and (b) implications, if any, for Tcl semantics ? /Ashok |
From: <apn...@ya...> - 2025-01-31 06:42:27
|
Donal, Thanks for the reply. I do think though that what you described is far more capable and sophisticated than what I was envisioning (hence my comment in my previous post about generalized constraints being complex). But before commenting further, I should look at your const implementation in more detail to understand better what would be involved even for my "simpler" goals. /Ashok From: Donal Fellows <don...@ma...> Sent: Thursday, January 30, 2025 10:06 PM To: tcl...@li...; apn...@ya... Subject: Re: [TCLCORE] On type checking variables That's quite complicated as it stands; constant-ness is a single bit flag right now that is tested for before analysing the variable in any depth (i.e., before searching for traces) so it's pretty cheap. Binding a type is more complex; the current simplest mechanism for it is what we have for Tcl_LinkVar() and that involves traces and a way to unwind rejected writes as traces run after the fact. A type check would probably be better done through a pre-write authorization callback so that there's no need to unwind failures; we don't have such a thing right now so this would be new code to hang on the Tcl_Var ekeko. In the simplest cases we'd be able to limit that to a "conforms to type" check but it's only easy to write such things for basic types. I'd not expect a speed boost from doing this, not without some sort of smart compilation to remove the need to check the type in the first place (and possibly unbox the value, for simple-enough types such as the one in your example). Obviously, no such compiler exists at the moment as it would be dependent on the model changes sketched in my previous paragraph. In general, type constraints are complex because types are complex, as one inevitably wants to start describing types of compound objects like lists and dicts, which are a yawning abyss of tricky cases. It's a proper can of worms! (As evidence, I cite the enormous complexity introduced by typing in Python, not made easier by their different approach to mutability.) Basically, we can add type constraints pretty easily (one more callback type to run as part of a variable write prior to the variable being written) but defining the space of type constraints that can be enforced at that point is VERY HARD! And constants don't use that mechanism. They just deny all writes. Donal. _____ From: apnmbx-public--- via Tcl-Core <tcl...@li... <mailto:tcl...@li...> > Sent: Thursday, January 30, 2025 16:08 To: tcl...@li... <mailto:tcl...@li...> <tcl...@li... <mailto:tcl...@li...> > Subject: [TCLCORE] On type checking variables (Probably a question for Donal, but sending to the list in case folks have ideas / opinions for or against) The [const] command implementation in Tcl 9 essentially bars writes to specified variables. How hard would it be to loosen this constraint mechanism to instead only bar certain writes, for example, writing values that do not match a specified type? So, in Tcl 9 const x 42 set x 43 <- disallowed Similarly, Int32 x 42 Set x 43 <- allowed Set x abc <- disallowed Etc. A generalized constraint mechanism will introduce many complexities but just type check does not seem hard to implement. The motivation by the way is to simplify interfaces to external API's in other languages and data exchange by not having to carry typing tags. Any comments on (a) feasibility, and (b) implications, if any, for Tcl semantics ? /Ashok |