Hi,
I'm a bit sick of writing things like: numOfOccs := numOfOccs + 1 So I decided to create 2 methods (#inc and #dec) in Number, so I can write instead: numOfOccs inc. However, I'm having problems with the implementation of somethig this simple. I tried: Number>>inc self := self +1. But the compiler barfed. How can I do this? I'm I doing something silly? O:-) Thanks |
Fernando,
> I'm a bit sick of writing things like: > numOfOccs := numOfOccs + 1 > > So I decided to create 2 methods (#inc and #dec) in Number, so I can > write instead: > numOfOccs inc. > > However, I'm having problems with the implementation of somethig this > simple. I tried: > > Number>>inc > self := self +1. > > But the compiler barfed. How can I do this? I'm I doing something > silly? O:-) IIRC, SmallIntegers are encoded entirely in the object pointer, and won't like being turned into one of their peers. My usual reaction to this kind of thing is that if I am doing so many arithmetic calculations that anInteger++ is going to be of benefit, it probably should be in a C/C++ function living in a DLL. Another option is to do the work on background threads where speed is (usually) less of a concern. Have a good one, Bill -- Wilhelm K. Schwab, Ph.D. [hidden email] |
Bill Schwab wrote:
> IIRC, SmallIntegers are encoded entirely in the object pointer, and > won't like being turned into one of their peers. My usual reaction to > this kind of thing is that if I am doing so many arithmetic calculations > that anInteger++ is going to be of benefit, it probably should be in a > C/C++ function living in a DLL. Another option is to do the work on > background threads where speed is (usually) less of a concern. I wan't to save typing, not time. |
> I wan't to save typing, not time.
You still need to assign the new ref somewhere.. so you could do... val := val inc. Number>>inc ^self + 1 but personally I'd just stick with the val := val + 1. Live with the extra typing there, and think of all the extra typing you are saved because of the wonders of #do: |
In reply to this post by Fernando Rodríguez
Fernando wrote:
> > Bill Schwab wrote: > > >>IIRC, SmallIntegers are encoded entirely in the object pointer, and >>won't like being turned into one of their peers. My usual reaction to >>this kind of thing is that if I am doing so many arithmetic calculations >>that anInteger++ is going to be of benefit, it probably should be in a >>C/C++ function living in a DLL. Another option is to do the work on >>background threads where speed is (usually) less of a concern. > > > I wan't to save typing, not time. > Then you probably want to alter the parser so it can recognize your shorthand and expand it to standard Smalltalk syntax. R - |
Please remeber following:
Code are writen once but are read many time. So your effort to typing is very valuable. Whenever you spend your typeing effort, code is grown day by day to mature and more readable. |
In reply to this post by Fernando Rodríguez
Fernando,
> I'm a bit sick of writing things like: > numOfOccs := numOfOccs + 1 > > So I decided to create 2 methods (#inc and #dec) in Number, so I can > write instead: > numOfOccs inc. > > However, I'm having problems with the implementation of somethig this > simple. I tried: > > Number>>inc > self := self +1. > > But the compiler barfed. How can I do this? I'm I doing something > silly? O:-) I think some of the other replies to this request sort of imply that it's not possible to do it. It certainly isn't possible to assign to the pseudo variable self but it should be perfectly feasible to modify the internal state of the number in question to add (or subtract) one to it. Typically, you would have to write an implementation for each kind of number. However, because of the special representation of SmallIntegers (where the value is encoded directly in the object pointer) it will not be possible to do this for these objects. This would rather limit the usefulness of #inc and #dec since they would only work on a subset of all numbers. Another possibility, although one to be frowned upon, would be to use #become:. I think this should be okay (although I always get confused with pointer swizzling) but it still wouldn't work correctly for SmallIntegers. !Number methodsFor! inc self become: self +1! ! Try: 3.2 inc "Display It" (3/4) inc "Display it" 300000000002 inc "Display it" 100 inc "Display it - should fail" Please, please don't use this method in any real code! The use of #become: is frowned upon at the best of times; in many Smalltalks the implementation is very slow (although not Dolphin as it happens) and to use it "just to save a bit of typing" would be foolhardy. Anyway, coupled with the fact that it only works for a subset of numbers, I think it should just be consigned to the "interesting ideas" bin. -- Andy Bower Dolphin Support www.object-arts.com |
Andy Bower wrote:
> Fernando, > > >>I'm a bit sick of writing things like: >>numOfOccs := numOfOccs + 1 >> >>So I decided to create 2 methods (#inc and #dec) in Number, so I can >>write instead: >>numOfOccs inc. >> >>However, I'm having problems with the implementation of somethig this >>simple. I tried: >> >>Number>>inc >> self := self +1. >> >>But the compiler barfed. How can I do this? I'm I doing something >>silly? O:-) > > > I think some of the other replies to this request sort of imply that > it's not possible to do it. It certainly isn't possible to assign to > the pseudo variable self but it should be perfectly feasible to modify > the internal state of the number in question to add (or subtract) one > to it. Typically, you would have to write an implementation for each > kind of number. The other alternative is to try and use context magic to modify the variable holding the integer. Turns out to be really hard to get right. But we had fun discussing this in February of 2002. See http://groups-beta.google.com/group/comp.lang.smalltalk/browse_thread/thread/8784d2420e248618/fc2b9d4369614465?q=comp.lang.smalltalk+localAt:&rnum=2&hl=en#fc2b9d4369614465 or Google groups for comp.lang.smalltalk localAt: and choose the "Reified variables (was: += for Smalltalk)" thread. -- _______________,,,^..^,,,____________________________ Eliot Miranda Smalltalk - Scene not herd |
In reply to this post by Fernando Rodríguez
Fernando wrote:
> Hi, > > I'm a bit sick of writing things like: > numOfOccs := numOfOccs + 1 > > So I decided to create 2 methods (#inc and #dec) in Number, so I can > write instead: > numOfOccs inc. > > However, I'm having problems with the implementation of somethig this > simple. I tried: > > Number>>inc > self := self +1. > > But the compiler barfed. How can I do this? I'm I doing something > silly? O:-) > > Thanks > Hi Fernando, Not silly at all. Just harder than expected - a rarity in Smalltalk. You've encountered the (in)famous prestidigitation pattern known as "the Incomplete Metaphor Illusion" Since #SmallIntegers are encoded within what would otherwise be an objectPointer, most attempts to accomplish your desired result will not pan out. One could argue that the VM should complete any illusions it creates, and that #become: should, therefore, work on SmallIntegers, too. If the reference was shared, well then sure, but it isn't, right? And then one might hear something like "Ha - what about literal frames?" but one parries with "premature optimization, it seems", or something. One might even prevail. In which case, you could just add Number>>inc ^self become: self + 1 and the like, as referred to elsewhere. Seems reasonable - possibly even elegant. {You'll have an easier time convincing yourself of this if your VM happens to treat the unadorned #become: as a one-way-become}. You might even consider submitting a bug report. ;-) Meanwhile, as long as you are only trying to reduce excess typing when *manipulating* numeric quantities (as you would in C, e.g.), there is a relatively simple solution to the problem. You may have seen a similar solution in another context... original := numOfOccs. numOfOccs := numOfOccs asFernandoNumber. numOfOccs inc. self assert: [numOfOccs value > original]. numOfOccs dec. self assert: [numOfOccs value == original]. If the above usage is a reasonable facsimile of what you had in mind, just use the FernandoNumbers. (If your image doesn't contain the definition, you might want to implement them yourself. Depends - are you going to do this a lot? ) Regards, -cstb ===+ Number>>asFernandoNumber ^FernandoNumber from: self >>value ^self FernandoNumber class>> self superclass: Object ; iVars: 'nValue' class>>from: aNumber ^self new from: aNumber >>from: aNumber nValue isNil ifFalse: [^self errorAlreadySet]. nValue := aNumber >>inc ^nValue := nValue +1 >>dec ^nValue := nValue -1 >>value ^nValue ===+ You'll probably want FernandoNumbers to behave like regular numbers in most other respects, so you'll want to transliterate most of the methods of class Number: >>+ aNumber ^value + aNumber >>- aNumber ^value - aNumber >>understandsArithmetic ^true ...etc --- |
cstb wrote:
> Number>>inc > ^self become: self + 1 And then what would happen if you sent #inc to 1. Would #inc thereafter increment by two ? ;-) BTW, I think that a better way to understand this "problem" is not to focus on what Integers can and cannot do, but to reflect that the ++ and -- operators from the C-language family do not operate on /values/ but on /variables/. That's to say that they don't belong in the same group of operators as, say, + or -, but in the family of assignment operators, =, +=, and so on. Smalltalk doesn't reify variables, so you cannot send messages to them, but only to the objects referred to /by/ those variables. If Smalltalk did have a way of referring to the variables themselves as objects in their own right, then Fernando's ambition would be simple to achieve, just add: inc self value: self value + 1. to the definition of Variable. -- chris |
Free forum by Nabble | Edit this page |