I'm trying to translate some C++ code into Sqeak (it's old, and doesn't use the stl). One of the first things it does is define a class called Vector. This appears to be the same as the FloatArray class...but not really the same.
What I want to do is ... well, it OUGHT to be subclassing of FloatArray, but that class uses a bunch of primitive operations, so I can't really tell all that it's doing. Still... The main thing I'm doing here is the same thing that's being done in FloatArray, but with different names for the operations. So I usually want to use the new names as labels for the old operation.
Is there a better way to do this than just subclass FloatArray and have each new method return the old method's calculation? When I get to Matrix, not only does it have the same name as an existing class (of which it apparently *ought* to be a subclass), but it implements methods like +, where Matrix says add: doNotImplement.
Suggestions?
Charles D Hixson wrote:
I'm trying to translate some C++ code into Sqeak (it's old, and doesn't use the stl). One of the first things it does is define a class called Vector. This appears to be the same as the FloatArray class...but not really the same.
What I want to do is ... well, it OUGHT to be subclassing of FloatArray, but that class uses a bunch of primitive operations, so I can't really tell all that it's doing. Still... The main thing I'm doing here is the same thing that's being done in FloatArray, but with different names for the operations. So I usually want to use the new names as labels for the old operation.
Is there a better way to do this than just subclass FloatArray and have each new method return the old method's calculation? When I get to Matrix, not only does it have the same name as an existing class (of which it apparently *ought* to be a subclass), but it implements methods like +, where Matrix says add: doNotImplement.
Suggestions?
I wouldn't subclass FloatArray. Instead I'd create a class Vector with an instance varaible (probably of type FloatArray) and delegate method in Vector to the object held by the ivar. This is the classic "adapter" pattern. Most of your methods will be trivial resends but this will give you a chance to deal with more significant differences between your "Vector" and Squeak's FloatArray.
As for your Matrix problem...a couple points:
1) Name conflicts are not uncommon. I prefix my class names with two letter project identifiers (SCMatrix, for example). Yes it is ugly but Squeak doesn't currently have namespaces so its all we've got.
2) The reason Matrix>>add: is a shouldNotImplement is because add: doesn't mean what you think. It means add in the collection since, that is, add an element to the matrix which makes no sense. Matrix does have an implementation of the + method (through its superclass) so the following code works as expected:
m1 := Matrix rows: 3 columns: 2 contents: #(1 2 3 0 9 8). m2 := Matrix rows: 3 columns: 2 contents: #(4 5 6 7 8 9). m1 + m2
Again, I'd suggest an adapter class if you want to preserve the API of your C++ classes in your Smalltalk code.
Hope this helps...
David
David Shaffer wrote:
C...
Again, I'd suggest an adapter class if you want to preserve the API of your C++ classes in your Smalltalk code.
Hope this helps...
David
Well, that's where I want to start. Presumably in the course of the translation I'll get a better idea of what the thing is doing, and possibly then I'll refactor. (I started off using the adaptor pattern, but as it started getting messy, I thought I ought to check and ascertain that this was, indeed, the correct approach. While writing to ask about it was really the first time I had thought that subclassing might be the best approach.)
One other suggestion that might work - create a category for your methods right on FloatArray. Put them in a category *projectname. You should probably read up on PackageInfo to understand how naming conventions work.
http://beta4.com/squeak/aubergines/docs/packageinfo.html
On May 10, 2006, at 1:38 PM, Charles D Hixson wrote:
David Shaffer wrote:
C...
Again, I'd suggest an adapter class if you want to preserve the API of your C++ classes in your Smalltalk code.
Hope this helps...
David
Well, that's where I want to start. Presumably in the course of the translation I'll get a better idea of what the thing is doing, and possibly then I'll refactor. (I started off using the adaptor pattern, but as it started getting messy, I thought I ought to check and ascertain that this was, indeed, the correct approach. While writing to ask about it was really the first time I had thought that subclassing might be the best approach.)
Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
Todd Blanchard wrote:
One other suggestion that might work - create a category for your methods right on FloatArray. Put them in a category *projectname. You should probably read up on PackageInfo to understand how naming conventions work.
http://beta4.com/squeak/aubergines/docs/packageinfo.html
On May 10, 2006, at 1:38 PM, Charles D Hixson wrote:
David Shaffer wrote:
C...
Again, I'd suggest an adapter class if you want to preserve the API of your C++ classes in your Smalltalk code.
Hope this helps...
David
Well, that's where I want to start. Presumably in the course of the translation I'll get a better idea of what the thing is doing, and possibly then I'll refactor. (I started off using the adaptor pattern, but as it started getting messy, I thought I ought to check and ascertain that this was, indeed, the correct approach. While writing to ask about it was really the first time I had thought that subclassing might be the best approach.)
Hmmm.... That sounds like something a bit too advanced for my current state of ignorance. It's nice to know that the possibility exists, but perhaps I'd best take things a bit more slowly.
beginners@lists.squeakfoundation.org