Works in Squeak but not GNU (deepCopy?)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Works in Squeak but not GNU (deepCopy?)

Jimmy Johnson
I don't know where else to turn for help so...if one of you experts would have the time to look at this project and see if an obvious problem jumps out, and let me know, I would really appreciate it.  

The project is to write a rational number class and include units/dimensions (meters/length).  The system works fine in Squeak SmallTalk, but fails in gnu SmallTalk.  I'm guessing it has something to do with my use of deepClone.

Attached is a zip with three files.  A chunk file, a readme, and a file with output from execution using squeak.

Thanks again,

 JJJ 

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk

st_files.zip (16K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Works in Squeak but not GNU (deepCopy?)

Paolo Bonzini-2
On 11/18/2011 01:42 PM, Jimmy Johnson wrote:
> I don't know where else to turn for help so...if one of you experts
> would have the time to look at this project and see if an obvious
> problem jumps out, and let me know, I would really appreciate it.
>
> The project is to write a rational number class and include
> units/dimensions (meters/length).  The system works fine in Squeak
> SmallTalk, but fails in gnu SmallTalk.  I'm guessing it has something
> to do with my use of deepClone.

Yes, that's correct.  deepCopy is not standardized, but it traditionally
made only a 1-level deep copy.

The better way to do it in GNU Smalltalk is to use "copy" and add
postCopy methods such as

Unitable>>postCopy
        unitsImp := unitsImp collect: [ :each | each copy ]

DimensionedRational>>postCopy
        unitsImp := unitsImp copy
       
etc.

 From a quick read of the code I'm not sure where you actually need
deeper copies, so the above is likely incomplete or incorrect.

As an aside, I suggest that you replace all uses of "self length" with
simply "1" (why is length special?) and merge the Constants and
Dimensionable classes.  Also, the Comparable class is exactly the same
as Magnitude, except that it doesn't test the precondition that the
classes are the same.  In general such preconditions hamper the usage of
polymorphism, so they're not used in Smalltalk.

Paolo

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Works in Squeak but not GNU (deepCopy?)

Jimmy Johnson
Is there a way to look at / edit the code of deepCopy in Object?



________________________________
 From: Paolo Bonzini <[hidden email]>
To: [hidden email]
Sent: Friday, November 18, 2011 10:49 AM
Subject: Re: [Help-smalltalk] Works in Squeak but not GNU (deepCopy?)
 
On 11/18/2011 01:42 PM, Jimmy Johnson wrote:
> I don't know where else to turn for help so...if one of you experts
> would have the time to look at this project and see if an obvious
> problem jumps out, and let me know, I would really appreciate it.
>
> The project is to write a rational number class and include
> units/dimensions (meters/length).  The system works fine in Squeak
> SmallTalk, but fails in gnu SmallTalk.  I'm guessing it has something
> to do with my use of deepClone.

Yes, that's correct.  deepCopy is not standardized, but it traditionally made only a 1-level deep copy.

The better way to do it in GNU Smalltalk is to use "copy" and add postCopy methods such as

Unitable>>postCopy
    unitsImp := unitsImp collect: [ :each | each copy ]

DimensionedRational>>postCopy
    unitsImp := unitsImp copy
   
etc.

From a quick read of the code I'm not sure where you actually need deeper copies, so the above is likely incomplete or incorrect.

As an aside, I suggest that you replace all uses of "self length" with simply "1" (why is length special?) and merge the Constants and Dimensionable classes.  Also, the Comparable class is exactly the same as Magnitude, except that it doesn't test the precondition that the classes are the same.  In general such preconditions hamper the usage of polymorphism, so they're not used in Smalltalk.

Paolo

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Works in Squeak but not GNU (deepCopy?)

Paolo Bonzini-2
On Fri, Nov 18, 2011 at 18:42, Jimmy Johnson <[hidden email]> wrote:
> Is there a way to look at / edit the code of deepCopy in Object?

It is in /usr/share/gnu-smalltalk/kernel/Object.st or similar.

You can modify it by putting in your file code like

!Object methodsFor: 'copying'!

deepCopy
    ...
! !

However, as far as I remember Squeak does have #postCopy, so switching
to it would probably be a better plan.

Paolo

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk