From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Maciek Godek" Newsgroups: gmane.lisp.guile.user Subject: SOS: Simple Object System Date: Sun, 14 Sep 2008 00:42:25 +0200 Message-ID: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_145491_253711.1221345745386" X-Trace: ger.gmane.org 1221345760 27913 80.91.229.12 (13 Sep 2008 22:42:40 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 13 Sep 2008 22:42:40 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sun Sep 14 00:43:36 2008 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1Kedq3-0008Ia-R6 for guile-user@m.gmane.org; Sun, 14 Sep 2008 00:43:36 +0200 Original-Received: from localhost ([127.0.0.1]:47911 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Kedp3-0002WT-7O for guile-user@m.gmane.org; Sat, 13 Sep 2008 18:42:33 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Kedoz-0002UF-24 for guile-user@gnu.org; Sat, 13 Sep 2008 18:42:29 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Kedox-0002SC-E4 for guile-user@gnu.org; Sat, 13 Sep 2008 18:42:28 -0400 Original-Received: from [199.232.76.173] (port=38526 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Kedox-0002S9-8p for guile-user@gnu.org; Sat, 13 Sep 2008 18:42:27 -0400 Original-Received: from rv-out-0708.google.com ([209.85.198.249]:41805) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Kedox-0001Rt-CI for guile-user@gnu.org; Sat, 13 Sep 2008 18:42:27 -0400 Original-Received: by rv-out-0708.google.com with SMTP id k29so1622773rvb.6 for ; Sat, 13 Sep 2008 15:42:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type; bh=t15e5AFI21YJVSMu1iPI4Cjdq/URPLMedTzxkQVka4I=; b=IzuenjnJ6SlsnOmtBQ22VJoVBcHoJPfF9GI+fa8sYhHwuAJZL0a9Q720Sp/3cWf5ce fLfxdlXwOfy6buY/bmpGGIrtwbyU4xLgFl/WV0LpVO0L6boy10adaJq7ZrcLQr39PDN+ lhBgIv9P1nvZeGVGwa3ZSsRtjdG8HjAvj3D+k= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type; b=vNlh0HWHtcrKOPJzT5bFtK4V7vxfhp6J4+p4YsCCE4QX60r5h1aHYXYh13fe2cmfBL D4MYVGZpPN2kXUOUq/dnJs0T7biXT+GpPFSQIwY4wzS3RI0GgyySZZ45BpmJgZDRMuUV 8LqGfAZUj50crb/r0DMMzAWb6s1SsFQclV6qU= Original-Received: by 10.142.186.9 with SMTP id j9mr2053443wff.115.1221345745396; Sat, 13 Sep 2008 15:42:25 -0700 (PDT) Original-Received: by 10.142.141.17 with HTTP; Sat, 13 Sep 2008 15:42:25 -0700 (PDT) X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:6766 Archived-At: ------=_Part_145491_253711.1221345745386 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi, Using some hints you gave me, I've implemented a really tiny object system -- and I would like to know your opinion ("why it's still better to use goops" :D) The notation for defining classes is following (using example of a sphere): (define sphere (class '(x y radius) '((move (dx dy) (set! x (+ x dx)) (set! y (+ y dy))) (scale (factor) (set! radius (* factor radius)))))) To make an instance, you simply use instance-of: (let ((S (instance-of sphere))) ... ) you can supply initial values to the object's props and call it's methods (let ((S (instance-of sphere 1 2 3))) (in S '(move 1 1)) ;move the sphere from (1 2) to (2 3) (in S '(scale 5)) (get S 'radius)) ; returns 15 The implementation is very simple: every object is a vector. Its first field is always a reference to the class, and the remaining fields are object's state (in the example, the values of x, y and radius of S) A class definition is a vector consisting of: hashmap N from property names to vector indices (in object) hashmap M from member function names to their indices in F vector F of member functions There's nothing surprising in here, and won't be. It has, as I can tell, a few advantages over goops -- mainly, storing objects as vectors allows for an efficient and convenient object treating from C level, so boatmen should be satisfied. Secondly, people accustomed to the object.method() notation (like myself) won't feel lost and the global namespace will be kept clean. The system certainly isn't as complex as goops and doesn't handle types (in general) so exquisitely. Also, it's unable to support multiple inheritance efficiently (single inheritance isn't supported as well, but this could be done quite easily if needed), but that's not my point. I just wanted to ask if you have any comments or remarks to share with (I know I'm not the first guy to implement a thing like this). I am currently using this system to implement another system (for networked objects -- I wrote about it in another post) and so far it caused no trouble. I attach the working implementation (if anyone's interested). For those who got this far (yes, that would be... you!), thanks for your attention :D M. ------=_Part_145491_253711.1221345745386 Content-Type: application/octet-stream; name=sos.scm Content-Transfer-Encoding: base64 X-Attachment-Id: f_fl2toxar0 Content-Disposition: attachment; filename=sos.scm Cih1c2UtbW9kdWxlcyAoc3JmaSBzcmZpLTEpKQoodXNlLW1vZHVsZXMgKHNyZmkgc3JmaS0xNykp Cih1c2Utc3ludGF4IChpY2UtOSBzeW5jYXNlKSkKCgooZGVmaW5lLXN5bnRheCBsZXQtYWxpYXMg CiAgKHN5bnRheC1ydWxlcyAoKSAKICAgICgoXyAoKGlkIGFsaWFzKSAuLi4pIGJvZHkgLi4uKSAK ICAgICAobGV0LXN5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