help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Something like an array (list) of a class


From: thierry . volpiatto
Subject: Re: Something like an array (list) of a class
Date: Sun, 12 Apr 2009 19:15:49 +0200
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.92 (gnu/linux)

Hi Drew,
"Drew Adams" <drew.adams@oracle.com> writes:

>> >> > I would like to make some datadriven functionality. Is 
>> >> > there a way to make sure a list is filled with a certain
>> >> > type of data. I would like to make sure that every element
>> >> > of my list contains: a type, a description, a functionname
>> >> > and room to store a string. Is this possible?
>> >>
>> >> ,----
>> >> | ELISP> (setq B '(2 "some text" message nil))
>> >> | (2 "some text" message nil)
>> >> |
>> >> | ELISP> (if (and (find-if 'numberp B)
>> >> |                 (find-if 'stringp B)
>> >> |                 (find-if 'functionp B)
>> >> |                 (null (car (last B))))
>> >> |            t
>> >> |            nil)
>> >> | t
>> >> `----
>> >
>> > Partly. This is an 'object' and not a list of objects. But I
>> > understand correctly that it is not possible to define real objects?
>> > What I would like is something like (C++ code):
>> >
>> > class Object {
>> >     String type;
>> >     String description;
>> >     String functionname;
>> >     String displayString;
>> > }
>> >
>> > std::vector <Object> objectVector;
>> >
>> > When given a certain type I then need to retreive the 'object' from
>> > the list to use.
>> 
>> There is no class in elisp but you have defstruct (be sure to 
>> require 'cl)
>> 
>> ,----
>> | ELISP> (defstruct Object type description functionname 
>> |        displaystring)
>> |        Object
>> | ELISP> (defvar test-obj (make-Object :type 1 :description 
>> |         "test" :functionname 'message :displaystring nil))
>> | test-obj
>> | ELISP> (Object-type test-obj)
>> | 1
>> | ELISP> (Object-functionname test-obj)
>> | message
>> | ELISP> (Object-description test-obj)
>> | "test"
>> `----
>
> Various books and Web sites about Lisp show you how to implement objects in
> Lisp, depending on what you mean by "object" (abstract data type? mutable
> object? classes? inheritance?...).
>
> In addition to Common Lisp's `defstruct', there is the Common Lisp Object 
> System
> (CLOS), but Emacs Lisp support for it is wanting. (From the Elisp manual: 
> "Some
> features are too complex or bulky relative to their benefit to Emacs Lisp
> programmers. CLOS and Common Lisp streams are fine examples of this group.")
>
> One thing you might also consider (again, depending on what you need/want), is
> that Emacs Lisp does provide a type-checking mechanism - in the context of
> Customize. This is often overlooked or under-exploited. Customize options can 
> be
> complex structures whose parts are well typed, and compile-time and runtime
> type-checking are available. In addition, you can define initialization and
> set/put methods/triggers.
>
> Whether you want to use options for your data structures in general is another
> question. You would be exploiting the type-definition and type-checking 
> features
> of Customize without necessarily wanting to create user-visible options. But
> this type manipulation is an existing and powerful Emacs-Lisp feature that you
> can use in ways other than those originally intended.
>
Very interesting, did you already use this kind of mechanism in some of
your programs ?

-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France




reply via email to

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