[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: One more question about elisp

From: Pascal J. Bourguignon
Subject: Re: One more question about elisp
Date: Sat, 07 Nov 2009 18:39:42 +0100
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin)

Francis Moreau <address@hidden> writes:

> On 7 nov, 02:40, LanX <address@hidden> wrote:
>> On 6 Nov., 22:05, Francis Moreau <address@hidden> wrote:
>> > It sounds strange to me (knowing C, python) to use hash tables to
>> > structure data.
>> You never used dicts in python for structured data???
> No since python is known to be an object oriented language, if I want
> to create an object, I'm defining a new class. I don't use a hash
> table for that.
> C allows to define new type. So I won't use a hash table to do that.
> But I can understand that elisp has no way to create new types, 

This is wrong.

> hash tables can serve the purpose although I'm wondering how the code
> will be readable (yes I already found elisp quite hard to read).

Actually, lisp code is more readable because there is no  impedence
mismatch between primitive and user defined data types.

You should read SICP:

         Structure and Interpretation of Computer Programs

The code will be readable because you will be using functional
abstraction: you will defined, (or more usually, have defined)
functions to access your abstract data type, the implementation of
which won't be relevant (unless you have some efficiency bottleneck,
but this remains to be proved by a profiler).

Said otherwise, you never use directly primitive functions such as
car, cdr, aref, or gethash, etc.  Always wrap them by the functions of
your own abstract data types.

>> IIRC an object instance  (like in your example) is actually just a
>> dictionary and "dictionary" is just the python way to say hash table.
> That's an implementation detail. The way you access fields of an
> object through a hash table doesn't mean that an object is equivalent
> to a hash table.

The same occurs in emacs lisp.

You write:

(require 'eieio)

(defclass person ()
  ((name :initarg :name :type string :accessor person-name)
   (age  :initarg :age  :type integer :accessor person-age)))

(defmethod print-object ((self person) stream)
  (princ (format "#<person named %S aged %S>" 
                 (person-name self) (person-age self))

(print-object (make-instance 'person :name "Mickey" :age 120) (current-buffer))

#<PERSON NAMED "Mickey" AGED 120>

You don't have to know that objects are implemented as vectors in emacs lisp.

__Pascal Bourguignon__

reply via email to

[Prev in Thread] Current Thread [Next in Thread]