Path: utzoo!utgpu!watmath!maytag!watvlsi!fchan From: fchan@watvlsi.waterloo.edu (Francis Chan) Newsgroups: comp.lang.smalltalk Subject: Re: subclasses of Float in Smalltalk/V Message-ID: <4423@watvlsi.waterloo.edu> Date: 20 Jun 89 13:35:41 GMT References: <56225@linus.UUCP> Organization: U of Waterloo, Ontario Lines: 66 In article <56225@linus.UUCP> sdl@linus.UUCP (Steven D. Litvintchouk) writes: > >I currently own a copy of Smalltalk/V for the Mac. > >I am trying to create some subclasses of Float. For example, subclass >Degree, which would be constrained to have values in the range 0.0 .. >360.0. (For example, adding an instance of Degree to another instance >of Degree would return the sum mod 360.0.) > >The problem is: How can you create instances of class Degree? I >tried: > > Degree fromInteger: 50 > >But fromInteger is apparently a primitive which returns a Float. > >I also tried > Degree new > >But Smalltalk/V considers the expression (Float new) to be an error. > >I guess the problem is how to implement a conversion method for class >Float called "asDegree," which will return an instance of class Degree >equal to the Float number mod 360.0. I see no way to accomplish this >in Smalltalk/V. > > >Any ideas? > > >Steven Litvintchouk >MITRE Corporation >Burlington Road >Bedford, MA 01730 I ran into a similar problem trying to create a subclass for Integer. In the end I gave up doing trying to make the new class a subclass of Integer simply because I couldn't readily modify the primitives that handle the creation of new instances. Finally, I made a new class under Number instead of under Integer that used an instance variable to store the value. This is somewhat unsafe since this value can be easily changed thus violating the concept of a number (an object whose value is a constant). It's ugly but it works. Once you have an instance variable it is fairly easy to get an instance of Degree to show its value instead of `Degree' by overriding the printOn: method. In this case: printOn: aStream value printOn: aStream will do the job where value is the instance variable holding the degree value of the instance. If anybody can tell me how Float or Integer class instances are stored, modified, etc.. I would appreciate it a lot. This is using ST V/286 and not the Mac version but perhaps a general treatise may be useful as well. Francis Chan SWEN University of Waterloo