Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2012-03-27
15:28 • Closed ticket [3474460fff]: targetObject should be optional for oo::copy plu... artifact: 17f2f2f3ae user: dkf
2012-02-14
06:00 • Ticket [3474460fff]: 4 changes artifact: 83a1116078 user: basilik99
2012-02-13
20:41 • Ticket [3474460fff]: 5 changes artifact: 1aedd8b2f4 user: dkf
2012-02-07
02:05 • Ticket [3474460fff]: 4 changes artifact: 0a8d9a19e2 user: basilik99
2012-01-30
20:46 • Ticket [3474460fff]: 4 changes artifact: 1700d11811 user: dkf
20:33 • Ticket [3474460fff]: 4 changes artifact: f4658b4303 user: dkf
2012-01-28
21:20 • Ticket [3474460fff]: 4 changes artifact: 97389e40f6 user: basilik99
02:19 • Ticket [3474460fff]: 4 changes artifact: 700c4fbcfc user: andreas_kupries
01:58 • Ticket [3474460fff]: 4 changes artifact: 0c05c27d5a user: basilik99
2012-01-27
17:38 • Ticket [3474460fff]: 4 changes artifact: 271b857980 user: dkf
17:38 • Ticket [3474460fff]: 4 changes artifact: 8d158ca26e user: dkf
17:37 • Ticket [3474460fff]: 4 changes artifact: 4c485d33cb user: dkf
17:35 • Ticket [3474460fff]: 4 changes artifact: 9954d17570 user: dkf
17:35 • Ticket [3474460fff]: 4 changes artifact: b8a5d17b6a user: dkf
06:40 • Ticket [3474460fff]: 4 changes artifact: dd6a8ff2e7 user: basilik99
2012-01-26
07:05 • Ticket [3474460fff]: 4 changes artifact: 4171739aaa user: sebres
06:42 • Ticket [3474460fff]: 4 changes artifact: 5462916069 user: basilik99
2012-01-25
21:52 • Ticket [3474460fff]: 4 changes artifact: 7c8e96ff03 user: dkf
2012-01-24
03:35 • Ticket [3474460fff]: 4 changes artifact: 4790114238 user: basilik99
2012-01-22
22:28 • Ticket [3474460fff]: 4 changes artifact: 8d2c85a1d7 user: dkf
2012-01-16
14:41 • New ticket [3474460fff]. artifact: 605136bace user: basilik99

targetObject should be optional for oo::copy

Closed / Fixed3474460fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 15 years ago
Descriptionplain2012-01-16 14:41:27
Hi,

With ActiveState Tcl 8.6.0.0.295132 Beta 6, when issuing:

oo::copy Class1

It gives the error:

"must supply a name when copying a class"

However, the documentation tells that the second parameter is optional. I would indeed expect Tcl to generate a new name such as ::oo::Obj34.

Note: this is useful to include the same class as a mixin in each class of a hierarchy. It seems Tcl only mixins a class once per hierarchy and not once per class having "mixin MyMixClass" in that hierarchy. So putting "mixin [oo::copy MyMixClass]" in each class of the hierarchy should solve the problem. This allow to add extra code to a method between each stage of the hierarchy.

Thanks,
Etienne
dkfplain2012-01-22 22:28:20
Making the name optional would certainly be possible. (It would also be possible to allow the specifying of the name of the target object's namespace.)

More generally though, there should be also some callback/method to allow an object to customize itself after being copied. Hmm.
basilik99plain2012-01-24 03:35:34
Hi,

Indeed! I was about to open another ticket (probably a feature request) about the fact that given a class A which is an instance of class X (rather than oo::class as usual), then when copying A to B, the constructor of X does not get called. I think that would be the natural hook.

I also had another concern: when I first used oo:::copy, I expected the new class to be created the same way as the first one. Eg:

oo::class create CLASS1 \
{
   superclass MYBASE [oo::copy STORABLE [expr rand()]]  ;# rand is because targetObject is not optional
   variable EXPRESSIONS

   puts "We are building a class."

   constructor {pL_EXPRESSIONS} \
   {
      set EXPRESSIONS $pL_EXPRESSIONS
   }

   method WRITE {} \
   {
      puts $EXPRESSIONS
   }
   export WRITE
}

oo::copy CLASS1 CLASS2
oo::define CLASS2 superclass MYBASE [oo::copy STORABLE [expr rand()]]

1) In that example, WRITE method on an object of CLASS1 works: the "variable" command given a class declaration makes that variable visible in all methods without having to write "my variable EXPRESSIONS" at the beginning of each method. That no longer works for CLASS2: the "variable" command does not seem copied...

2) Also, "rand" function does not get called once for CLASS1 and once for CLASS2. Its result is re-used when creating CLASS2. This forces me to override the superclasses of CLASS2 using oo::define command in order to generate a new copy of STORABLE class for CLASS2.

Similarly, the text "We are building a class." gets printed only for CLASS1, not for CLASS2.

In other words, it seems only the constructor, destructor and methods are copied. But it could be useful if all the code passed to "oo::class create" would be copied.

Tell me if I should open another ticket with these. Thanks,
Etienne
dkfplain2012-01-25 21:52:29
Fixed the issue with 'variable'; that was Just A Bug. Code is in trunk; will form part of next release.

I don't grok what you're going on about with rand().
basilik99plain2012-01-26 06:42:28
Hi,

Thanks for the correction about "variable" being copied.

The rand trick comes from a higher need: I have a hierarchy of classes that I want to serialize to a file. Suppose the following classes (baseclasses to subclasses): A -> B -> C -> D -> E. A given object of type E will be serialized when its WRITE method will be called. Each class can write specific info. But all classes will write its class type to the serialization output file.

I wanted to implement another class, STORABLE, which job would be to write these types to the file. With introspection, it is easy: look at the class name, go get a global constant of a name similar to it, output the value of that constant. So, I want the call to the WRITE method on an object of type E to give the following chain:
E's WRITE -> STORABLE's WRITE -> D's WRITE -> STORABLE's write -> ... -> STORABLE's WRITE -> A's WRITE.

1) I first tried to insert STORABLE as a mixin in the hierarchy, but STORABLE was accepted only once for the whole hierarchy (in the top superclass) and hence STORABLE's WRITE was called only once, not 5 times. Also, STORABLE's WRITE was called BEFORE the implementation of the class it is applied to: I could work with this, but it is not was I expected (however, I was new to mixins...).

2) The I tried using STORABLE as a superclass. Again, all superclass commands were ignored except the one in the topmost class. To TclOO to see its the same class, I made a copy of STORABLE each time I used it as superclass. And that's what explains the oo::copy. The rand is only because I wanted a different name for each STORABLE class copies.

There may be better ways to achieve my goal. Suggestions are welcome.

Thanks,
Étienne
sebresplain2012-01-26 07:05:53
can be useful for you:
http://wiki.tcl.tk/23444

you can extend your each class with method "serializable" and extend this code with checking is a class serializable by generating string of content. then simple write/read string into/from file.
basilik99plain2012-01-27 06:40:05
Hi,

Thanks sebres for this link. I looked at it and it could be good for a new project. However, the application which is deserializing my objects is already written and expects a particular format. In particular, it expects to find an ID for each class that composes an object O.

Suppose the class hierarchy A -> B -> C -> D -> E, E having no subclass. Then in the serialized binary code, there will be some bits indicating the object is of type A, then of type B, and so on, further refining until the leaf class is reached. Moreover, at some stages, additional information is added (i.e. B outputs the attributes it contains before continuing on the chain). That might not be the standard way to do deserialization, but I have to stick with that format.

Still, I am still wondering if TclOO allows such methods to be "inserted" between each class of a hierarchy...

DKF, will the targetObject bug be corrected? And did you think about the copy hook?

Thanks,
Étienne
dkfplain2012-01-27 17:35:11
In my build, the targetObject is optional and as far as I can tell, has been for a long time (it's optional at the point where the code was committed to Tcl). Can you please tell me exactly what [package require TclOO] reports?
dkfplain2012-01-27 17:35:46
Oh, hang about. It's a _class_ you're copying.
dkfplain2012-01-27 17:37:52
Basically, I don't understand why you would want an unnamed class. What sort of rationale is there for doing this? (I now comprehend what you're doing with rand(), but not *why*.)
dkfplain2012-01-27 17:38:10
IP - Comment Removed: 130.88.1.31
dkfplain2012-01-27 17:38:19
IP - Comment Removed: 130.88.1.31
basilik99plain2012-01-28 01:58:33
Because to implement the serialization as I described, STORABLE's WRITE method must be called at each stage. But putting STORABLE as a superclass of classes A, B, C, D and E will not work: only class A will have STORABLE as superclass. Hence, STORABLE's WRITE will be called only once, after A's WRITE.

By copying STORABLE to an unnamed class, TclOO allows each of A, B, C, D and E to inherit from STORABLE, or to be more precise, to a distinct copy of STORABLE.

In short, I wanted the following inheritance tree:
STORABLE
  \
   A     STORABLE
     \     /
       B     STORABLE
        \    /
         ...     STORABLE
           \    /
             E
andreas_kupriesplain2012-01-28 02:19:10
Could this type of thing be handled better as a class or object mixin ?
basilik99plain2012-01-28 21:20:02
Again, putting STORABLE as a mixin for classes A to E will make TclOO accept STORABLE as a mixin only for A. At least, STORABLE's WRITE method is only called once, and before A's WRITE: E's WRITE -> D's WRITE -> C's WRITE -> B's WRITE -> STORABLE's WRITE -> A's WRITE.
dkfplain2012-01-30 20:33:15
I'm curious as to why there's a need to make all those STORABLEs. In particular, what's the benefit to putting those in their own classes as opposed to having the WRITE as a method on A...E directly? (When I wrote a TclOO serializer I didn't store the classes themselves, but that was due to the use-case I had in mind. That didn't require this baroque structure.)

You could arrange for the creation of the method to be done by a metaclass if writing it directly is too onerous. Metaclasses (classes that are subclasses of oo::object) are essentially a way of massively customizing the class manufacturing process and let you do some *really* funky stuff.
dkfplain2012-01-30 20:46:10
My serializer would just introspect across the whole state — easy because TclOO (deliberately) doesn't play all that many tricks with scoping — and save it out. It included a call to a special method to allow for custom control of serialization, which would be necessary for handling things like widgets, channels, etc.
basilik99plain2012-02-07 02:05:06
I am not sure to understand your question, but I wrote the STORABLE class (there is only one STORABLE class) to avoid copying-pasting code. Indeed, it writes to the output something similar to the class name (in fact, it is not a string, but a binary code). As Tcl can do introspection, STORABLE can look at the class name and write it down. This can be done with generic code that doesn't have to be modified if classes are added/removed in A->E hierarchy.
dkfplain2012-02-13 20:41:58
I plan to drop the check on whether a class is being no-name-copied (as part of overhaul of [oo::copy]). It was not a documented constraint and was never necessary. It's still very odd to me why you'd want to do such a complex arrangement, but I have no good reason to say "No" so I must say "Yes".
basilik99plain2012-02-14 06:00:40
Thanks for your answers in this ticket  and in "TclOO: variables not copied when mixins are copied - ID: 3485060". Note that I am not the one that can perform the review of the changes you proposed as I am not familiar with Tcl development itself.
Etienne
dkfplain2012-03-27 15:28:50
allow_comments - 1

Fixed with commit of TIP #397 implementation,
Log in (anonymous is fine) to add a comment.