init_ return value (from WIKI)

"Mark Hahn" <[email protected]>
Newsgroups gmane.comp.lang.prothon.user
Message-ID <[email protected]>
Mark Hahn Wrote:

Serge talked me into removing the init_ return value feature that replaced
the object being intialized with a totally new object. He did this by
pointing out that I should use call_ instead of init_ to do singletons. I
went through all my C code and removed all places that used this feature and
removed the feature itself.

I was working on the regression code tests for a release when I ran into
this Prothon code originally written by Lenard:
object ModInt(Int):
    modulo = 10
    def init_(i=0):
        Int.init_{self}(i % self.modulo)
    def add_(other):
        if self.modulo != other.modulo:
            raise TypeError
        return (self.protos_[0])(Int.add_{self}(other) % self.modulo)

object Mod16Int(ModInt):
    modulo = 16

object Mod16IntC(Mod16Int):
    cache = {}
    def init_(i):
        i = i % self.modulo
        if i in cache:
            print  cache[i] , 'from cache'
            return cache[i]
        obj = Mod16Int(i)
        cache[i] = obj
        print obj, 'new'
        return obj

Note how it uses the init_ return value feature to use a cache for init
values. I can't figure out an easy way to replace this code.
Serge: How do I fix this?

Mark Hahn Replied to his own post:
I guess it should be rewritten to use call_ instead of init_ just like my
singleton examples. I an so used to using init_ I forget I can just use
call_ anytime I want.

Serge replied:
You're right, just replace init_ with call_ in the class Mod16IntC, that's
all. However the original code does not create instances of the Mod16IntC
class, it just acts like a simple function, you can as well replace it with
cache = {}
def Mod16IntC(i):
    i = i % self.modulo
    if i in cache:
        print  cache[i] , 'from cache'
        return cache[i]
    obj = Mod16Int(i)
    cache[i] = obj
    print obj, 'new'
    return obj
Here is the version that creates Mod16IntC instances:
object Mod16IntC(Mod16Int):
    cache = {}
    def call_(i):
        i = i % self.modulo
        if i in cache:
            print  cache[i] , 'from cache'
            return cache[i]
        obj = Object.call_{self}(i)
        cache[i] = obj
        print obj, 'new'
        return obj
    def init_(i):
        return Mod16Int.init_{self}(i)
Testing:
i = Mod16IntC(8)
j = Mod16IntC(8)
m = Mod16IntC(1)
print((i + m) + j)

8 new
8 from cache
1 new
9 new
1 from cache

Serge Notes: The line obj = Object.call_{self}(i) looks ... (hmmm hard to
find the word) cumbersome? How about making Object.newInstance =
Object.call_, then one can write obj = self.newInstance_(i), it looks much
more readable. Comments?

Mark Hahn replied:
You can also use super.call_(i) which I just tested.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.