From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Kjetil S. Matheussen" Newsgroups: gmane.lisp.guile.user Subject: Re: Closure? Date: Mon, 14 Jul 2008 23:41:22 +0200 (CEST) Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7BIT X-Trace: ger.gmane.org 1216071744 26985 80.91.229.12 (14 Jul 2008 21:42:24 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 14 Jul 2008 21:42:24 +0000 (UTC) To: Maciek Godek , guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Jul 14 23:43:11 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 1KIVow-0002Op-NA for guile-user@m.gmane.org; Mon, 14 Jul 2008 23:42:58 +0200 Original-Received: from localhost ([127.0.0.1]:51558 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KIVo4-0001uS-If for guile-user@m.gmane.org; Mon, 14 Jul 2008 17:42:04 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KIVnz-0001u4-LV for guile-user@gnu.org; Mon, 14 Jul 2008 17:41:59 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KIVnw-0001tf-Uo for guile-user@gnu.org; Mon, 14 Jul 2008 17:41:58 -0400 Original-Received: from [199.232.76.173] (port=46763 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KIVnw-0001tc-Pz for guile-user@gnu.org; Mon, 14 Jul 2008 17:41:56 -0400 Original-Received: from smtp.getmail.no ([84.208.20.33]:49622) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1KIVnx-00059k-4R for guile-user@gnu.org; Mon, 14 Jul 2008 17:41:57 -0400 Original-Received: from pmxchannel-daemon.no-osl-m323-srv-009-z2.isp.get.no by no-osl-m323-srv-009-z2.isp.get.no (Sun Java System Messaging Server 6.2-7.05 (built Sep 5 2006)) id <0K4000507MXJRN00@no-osl-m323-srv-009-z2.isp.get.no> for guile-user@gnu.org; Mon, 14 Jul 2008 23:41:43 +0200 (CEST) Original-Received: from smtp.getmail.no ([10.5.16.1]) by no-osl-m323-srv-009-z2.isp.get.no (Sun Java System Messaging Server 6.2-7.05 (built Sep 5 2006)) with ESMTP id <0K4000EWTMX4KK10@no-osl-m323-srv-009-z2.isp.get.no> for guile-user@gnu.org; Mon, 14 Jul 2008 23:41:28 +0200 (CEST) Original-Received: from cm-84.215.136.96.getinternet.no ([84.215.136.96]) by no-osl-m323-srv-009-z1.isp.get.no (Sun Java System Messaging Server 6.2-7.05 (built Sep 5 2006)) with ESMTP id <0K4000MRCMX4STI0@no-osl-m323-srv-009-z1.isp.get.no> for guile-user@gnu.org; Mon, 14 Jul 2008 23:41:28 +0200 (CEST) In-reply-to: X-X-Sender: kjetil@ttleush X-detected-kernel: by monty-python.gnu.org: Solaris 10 (beta) 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:6658 Archived-At: On Mon, 14 Jul 2008, Maciek Godek wrote: > Kjetil S. Matheussen: > >> I think local-eval is necessary for making >> a namespace system of the below type without having to use >> codewalking macros to expand the bodies of functions: >> >> (define-namespace bank) >> (def-bank sum 0) >> (def-bank (add n) >> (set! sum (+ n sum)) ;; Note that it's enough to write "sum". >> >> (bank.add 50) >> bank.sum >> => 50 > > This certainly looks like trashing global namespace > (which isn't good in the long run) and doesn't allow you > to have object handlers (like many variables referring > to the same object) without additional quirks. > It's just an example of what local-eval can be good for. It's an example of a _namespace system_, and not an _OO system_. >> But for implementing a message passing OO system, >> it's easier to use macros and hash tables, plus >> that it probably performs much better: > > Pefrorms better than local-eval or better than > define-namespace? > It's much faster than local-eval since it's not calling eval for each method call. The overhead of calling a method in an OO system, based on hash tables and macros, is basically just the time it takes looking up the address of the function from a hash-table. For guile, which has a large overhead already, your program probably won't go any slower doing this compared to calling functions directly: (define (make-bank sum) (let ((attributes (make-hash-table))) (define dispatcher (lambda (which . rest) (apply (hashq-ref attributes which) rest))) (hashq-set! attributes 'sum (lambda () sum)) (hashq-set! attributes 'add (lambda (n) (set! sum (+ n sum)))) dispatcher)) (define bank (make-bank 0)) (bank 'add 2) (bank 'sum) => 2 And by using some macros, you can make the syntax look much prettier. >> (def-class >> (def-var sum 0) >> (def-method (add n) >> (set! sum (+ n sum))) >> >> (define bank (new )) >> (-> bank add 50) >> (-> bank sum) >> => 50 >> >> There's a bunch of these systems for scheme. >> The syntax above is used from >> http://snd.cvs.sourceforge.net/snd/cvs-snd/oo.scm?view=log >> >> Guile's own OO system called GOOPS is also very nice. >> GOOPS a quite verbose but very powerful and a lot >> more interactive. It's similar to CL's CLOS, which >> you should look at if you are not familiar with >> already. > > Well, I've read some documentation of GOOPS and then > I took a glimpse at its source. It has at least a few > disadvantages, for it is an object system implemented in > scheme -- it is therefore hard to access its objects from C > (while closures are easily accessible through > scm_local_eval) and it probably won't run as fast as > local-eval, at least conceptually. > local-eval is forced to interpret its argument, and is therefore, at least conceptually, very slow. I very much doubt GOOPS is that slow, but I don't know how GOOPS is implemented though. :-)