Re: [TCLCORE] Cloverfield references (was Re: Variable access)
by Andreas Leitgeb other posts by this author
May 20 2008 4:48AM messages near this date
Re: [TCLCORE] [Tcl9-cloverfield] Cloverfield references (was Re: Variable access)
|
Re: [TCLCORE] Cloverfield references (was Re: Variable access)
Frederic Bonnet <fbonnet@[...].fr> wrote:
> Andreas Leitgeb wrote:
> > And what, if I wanted to start from an empty list, and add
> > these sublists in a loop? How would I get it started? How
> > do I add a new element to be refer-able ?
> Another way to do it would be:
> set first 1st
> foreach next {2nd 3rd 4th} {
> lappend l3 ($&first $next)
> }
> puts $l3; # ({ref <id>}1st 2nd) ({ref <id>}{} 3rd) ({ref <id>}{} 4th)
Ah, ok, so now this is with external ref, then.
I'm however somewhat struck by odd, as to the auto-internalizing of refs,
and what I'd deduce as logical consequence:
set l3_copy $l3 ;# still with extref to "first"
# mod on $&l3_copy{0 0} reflects in $first and in $l3, right?
# Out of curiosity, is there a way to see that "first"'s
# value is currently ref-shared?
unset first; # assuming no previous further spreading of $&first
set l3_2ndcopy $l3
# mod on $&l3_2ndcopy{0 0} does *not* reflect in $l3, right?
> <id> being some identifier. But there's a subtlety in the sense that the
> reference is no longer intern but extern, ie it refers to an outer
> variable. So copying the value preserves the external reference.
And what if I did it the other way round:
set l4 (({ref a}1st 2nd) ({ref a}{} 3rd))
set ext $&l4{0 0}
# does this make the reference "a" external, such
# that a copy of $l4 made afterward would then still
# share the ref?
> > Second question:
> > how would I obtain a thoroughly ref-free snapshot of $l ?
> > (One, whose stringrep would then be: {{1st 2nd} {1st 3rd}})
> As a general rule, you can't, because structures are potentially
> self-referential.
> The real question is, why and in which case would you do such a thing?
I didn't think of cyclic lists. You're right, what I requested could be
quite hazardous :-)
Also acc'ing the [list {*}$v] approach for one-level snapshooting.
> > Third question:
> > If I wanted to write a procedure that works correct with such
> > mutables,
> First, don't confuse mutability and references,
Indeed, I was sloppy with my language :-)
I do know the difference.
> A structure with references can be used in an immutable way.
A structure with external refs can be used in an immutable way,
but still behaves like partially mutable, as you explained in
your answer to my first question. But that's just a sidenote,
not the point of this question.
> (assuming that Tcl's lreplace is kept as such, and converted to accept
> references as arguments).
If lreplace was thusly converted, why not lrange?
> # Cloverfield
> set i 1
> set ref $&i # ref is 1
> setref $&ref 2 # i is 3
2? 3? I guess, that's a typo :-)
Anyway, this would mean, that:
> proc strip {l} {
> setref $&l [lrange $l 1 end-1]
> }
would have answered my third question.
> Unless we decide that set takes references, but this is a
> significant departure from Tcl
It would obviously not be the first one in cloverfields history :-)
If "append $&i ..." were an acceptable departure, then
"set $&l ..." wouldn't be less so in my eyes.
> > Fourth question: [about serialization of internal references]
> Building circular structures: set v {ref root}(a {ref root}{})
:-)
> And now for fun, enter inline expansion {*}:
> set v {ref root}(a {*}{ref root}{})
That beats me. Is this still just discussion of "what would be"
or is there already a demo-implementation that accepts this?
I probably wouldn't allow that particular construction, since
I believe that this behaviour is really impossible to implement
generally.
e.g.:
proc b {} {puts "b called"}
set v {ref root}(a {*}{ref root}{} [b])
The second element (thus the whole structure) is immediately
inlined, before the third element has even be parsed. That
can't go.
Probably, the {*} would have to be strictly prevented from
applying to a {ref ...}, or at least from applying to an
"immature" ref.
> > Fifth question:
> > Flattening:
> > % set f1 [list {*}$v];# stringrep=??
> Each of v's elements are passed as value to [list], so:
> (1 2 3) (1 2 3) (1 2 3)
% set f1 [list {*}$&v];# stringrep=??
% set f2 [list $&v{0} $&v{1} $&v{2}]; # ...
refs retained or lost?
> > % set f2 [concat $v $v];# stringrep=??
> {ref <id1>}(1 2 3) {ref <id1>}{} {ref <id1>}{} {ref <id2>}(1 2 3) {ref <id2>}{} {ref <id2>
}{}
makes sense.
> However the following would work as expected:
> % concat $&v $&v
> {ref a}(1 2 3) {ref a}{} {ref a}{} {ref a}{} {ref a}{} {ref a}{}
that, too.
(and a new) Sixth Question:
Does this {...} "lindex"-syntax have a pendant for dicts?
How does treating an even sized list as a dict work with ref's
both for keys and values?
e.g. puts [dict get ({ref a}2 {ref a}{} 3 {ref a}{})]
I really wouldn't believe refs to work as keys.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Tcl-Core mailing list
Tcl-Core@[...].net
https://lists.sourceforge.net/lists/listinfo/tcl-core
Thread:
fbonnet
fredericbonnet
Neil Madden
fbonnet
Neil Madden
fbonnet
Lars Hellstrom
fbonnet
Neil Madden
fredericbonnet
David Welton
fbonnet
David Welton
Larry McVoy
Alexandre Ferrieux
Andreas Leitgeb
fbonnet
Neil Madden
Donal K. Fellows
Alexandre Ferrieux
Larry McVoy
Neil Madden
Gustaf Neumann
Neil Madden
Larry McVoy
Neil Madden
Alexandre Ferrieux
fbonnet
Neil Madden
Alexandre Ferrieux
Donal K. Fellows
Larry McVoy
Alexandre Ferrieux
Donal K. Fellows
Alexandre Ferrieux
|