Prev: Is there a IPTC and/or XMP tcl package (scripted or binary) out in the wild?
Next: How to profile a Tcl/Tk extension?
From: Alexandre Ferrieux on 17 Jan 2010 18:47 On Jan 18, 12:33 am, Andreas Leitgeb <a...(a)gamma.logic.tuwien.ac.at> wrote: > > [format %d $num] does not add a string rep to an integer $num > some new [format %O $obj] could do the same for any object. Instead of a new % specifier, a [dup] could do. Unless Donal threatens to beat me, even if I hide it in ::tcl::unsupported::duplicate :-/ -Alex
From: Alexandre Ferrieux on 17 Jan 2010 19:36 On Jan 17, 11:52 pm, Andreas Leitgeb <a...(a)gamma.logic.tuwien.ac.at> wrote: > Alexandre Ferrieux <alexandre.ferri...(a)gmail.com> wrote: > >> Is there even a way to duplicate an arbitrary obj? > > Yes, though a bit contorted: > > # first make a second reference > > set obj2 $obj > > # then do something to the 2nd > > append obj2 "" > > Ah, appending an empty string ... I had tried just [append obj2], and it > was a noop (i.e. didn't even create a string-rep). I vaguely remember the > discussion about [append var] and "$var" skipping stringification, so I'm > not very confident, that [append var "" "" "" ""] wouldn't someday be > optimized away, as well. Indeed, no guarantee :/ However, as of today, yet another (and somewhat simpler) hack that works is: set void "" set obj2 $void$obj What it does is: (1) obtain $obj's string rep if not already computed (2) set obj2 to a pure string with (a copy of) that string rep. Note that $void must be on the left. As Kevin notes, the $void on the right is optimized away in support of constructs like the K-free K combinator: $x[unset x]. -Alex
From: Andreas Leitgeb on 18 Jan 2010 05:29 Alexandre Ferrieux <alexandre.ferrieux(a)gmail.com> wrote: > On Jan 17, 11:52 pm, Andreas Leitgeb <a...(a)gamma.logic.tuwien.ac.at> >> Alexandre Ferrieux <alexandre.ferri...(a)gmail.com> wrote: >> >> Is there even a way to duplicate an arbitrary obj? >> > Yes, though a bit contorted: >> > # first make a second reference >> > set obj2 $obj >> > # then do something to the 2nd >> > append obj2 "" >> I'm not very confident, that [append var "" "" "" ""] wouldn't someday be >> optimized away, as well. > However, as of today, yet another (and somewhat simpler) hack that > works is: set void ""; set obj2 $void$obj Or even without a dummy var: set obj2 []$obj But that isn't the same as the append-thing. append does a copy on write first, so the original object doesn't get a string rep. *That* was the intention.
From: Andreas Leitgeb on 18 Jan 2010 06:02 Alexandre Ferrieux <alexandre.ferrieux(a)gmail.com> wrote: >> [format %d $num] does not add a string rep to an integer $num >> some new [format %O $obj] could do the same for any object. > Instead of a new % specifier, a [dup] could do. Unless Donal threatens > to beat me, even if I hide it in ::tcl::unsupported::duplicate :-/ My primary intention here was not so much cloning of objects, but rather extraction of a generated string-rep. I wouldn't want to waste cpu&memory making a copy of the original object, if that could be avoided. format seems like a good metaphor to me, in that it doesn't really force one to think about tcl object internals in the first place, but rather, at a high level convey the purpose of creating a text-description of something, that isn't meant to be *normally used* as a string. The implementation (once a format-specifier is agreed on) should be trivial: see, if the object already has a string rep. If so, just share that. Otherwise request a string-rep of the object, share it to a new object and then remove it from the original object. As I wrote, the real potential of this approach will show up later, if a maxlen field-modifier could further save the effort of creating the complete string-rep in the first place. Even later, then, the "#"-modifier could be used to format objects in different ways, that are more trimmed for human readability, than for reconstructability. That's already [format]'s job, but so far only for a few types) It's just dreams and brainstorming for now, but I like that vision.
From: Alexandre Ferrieux on 18 Jan 2010 07:32
On Jan 17, 8:04 pm, Andreas Leitgeb <a...(a)gamma.logic.tuwien.ac.at> wrote: > [...] > and the issue > with format %s perhaps soon fixed, and no other such problem currently > on radar. [format %s] fixed in HEAD 8.[56]. -Alex |