From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:38304) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SPhRn-00065W-0Q for qemu-devel@nongnu.org; Wed, 02 May 2012 17:50:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SPhRk-0003R4-Ln for qemu-devel@nongnu.org; Wed, 02 May 2012 17:50:54 -0400 Received: from mail-pz0-f46.google.com ([209.85.210.46]:54463) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SPhRk-0003Qk-Cb for qemu-devel@nongnu.org; Wed, 02 May 2012 17:50:52 -0400 Received: by dady13 with SMTP id y13so511399dad.5 for ; Wed, 02 May 2012 14:50:49 -0700 (PDT) Sender: Paolo Bonzini Message-ID: <4FA1AC32.8020301@redhat.com> Date: Wed, 02 May 2012 23:50:42 +0200 From: Paolo Bonzini MIME-Version: 1.0 References: <2474f1d2-b6e2-475b-a45e-2fadf28625e5@zmail13.collab.prod.int.phx2.redhat.com> <4FA19277.2030209@us.ibm.com> In-Reply-To: <4FA19277.2030209@us.ibm.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH 11/21] qdev: move bus properties to abstract superclasses List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Anthony Liguori Cc: liwp@linux.vnet.ibm.com, peter maydell , qemu-devel@nongnu.org, afaerber@suse.de Il 02/05/2012 22:00, Anthony Liguori ha scritto: >> >> Not really, in fact this kind of class-side data is really bread and >> butter >> of all dynamic languages, and it's how most of them implement >> polymorphism. >> They have an associative array (method names -> method bytecode for >> example) >> in each class on the hierarchy, and walk the hierarchy for each >> function call. > > I'm not aware of any language that does this. Python: class A: def a(self): return "a" class B(A): def b(self): return "b" B.__dict__ # {'__module__': '__main__', 'b': , '__doc__': None} print(B().b()) # b B.__dict__["a"] # Traceback (most recent call last): # File "", line 1, in # KeyError: 'a' print(B().a()) # a Smalltalk: 'abc' class # String String methodDictionary includesKey: #== # false Object methodDictionary includesKey: #= # true Ruby too: class A def a() end end class B < A def b() end end B.instance_methods # [:b, :a, :nil?, :===, :=~, ...] B.instance_methods(include_super=false) # [:b] but I don't know exactly how to access the dictionary here. > Many languages today have an associative array in the object. It's > filled out from a class definition during instantiation. This is what > allows monkey patching. That's just Javascript as far as I know. Python has an associative array in the object _and_ one in the class, and they aren't exactly the same thing: class A: def a(self): return "a" A.__dict__ {'a': , '__module__': '__main__', '__doc__': None} x = A() x.__dict__ # {} x.a() # 'a' x.__dict__["a"] = lambda self: self x.__dict__ # {'a': at 0x7f67c8dc1500>} x.a() # Traceback (most recent call last): # File "", line 1, in # TypeError: () takes exactly 1 argument (0 given) x.a('look, no implicit self anymore!') 'look, no implicit self anymore!' y = A() y.a() # 'a' Ruby has an associative array in the object and one in the class, and they work as expected: class A def a() end end x = A.new # x.a # nil x.define_singleton_method("a") { puts "i'm special" } x.a # i'm special # nil x.singleton_methods # [:a] y = A.new y.a # nil y.singleton_methods # [] > Classes are first class objects and can contain members, but I don't > know of any system where you actively look at the same field in a > super class for each class in the hierarchy. That's really trippy. Just because it's new to you. Smalltalk has been doing that for 30-odd years. Of course the VM has a lot of caching going on, but it's entirely internal. > If we move properties to Object, I'd rather just stick a property > pointer in TypeInfo and call it a day. I'm not thrilled about it, but > I feel a lot better about it the above. What if we add declarative links or children, for example? Adding more junk to TypeInfo is not the solution. Paolo