* Something like an array (list) of a class
@ 2009-04-10 20:32 Decebal
2009-04-11 8:08 ` thierry.volpiatto
[not found] ` <mailman.5118.1239437738.31690.help-gnu-emacs@gnu.org>
0 siblings, 2 replies; 14+ messages in thread
From: Decebal @ 2009-04-10 20:32 UTC (permalink / raw)
To: help-gnu-emacs
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?
Also what is the best way to fetch the element of the list that is of
a specific type (first field of the object)?
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Something like an array (list) of a class
2009-04-10 20:32 Something like an array (list) of a class Decebal
@ 2009-04-11 8:08 ` thierry.volpiatto
[not found] ` <mailman.5118.1239437738.31690.help-gnu-emacs@gnu.org>
1 sibling, 0 replies; 14+ messages in thread
From: thierry.volpiatto @ 2009-04-11 8:08 UTC (permalink / raw)
To: help-gnu-emacs
Hi Decebal,
Decebal <CLDWesterhof@gmail.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?
Maybe that help?
,----
| 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
`----
> Also what is the best way to fetch the element of the list that is of
> a specific type (first field of the object)?
The same with `find-if'
--
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France
^ permalink raw reply [flat|nested] 14+ messages in thread
[parent not found: <mailman.5118.1239437738.31690.help-gnu-emacs@gnu.org>]
* Re: Something like an array (list) of a class
[not found] ` <mailman.5118.1239437738.31690.help-gnu-emacs@gnu.org>
@ 2009-04-12 7:25 ` Decebal
2009-04-12 8:03 ` thierry.volpiatto
[not found] ` <mailman.5186.1239523825.31690.help-gnu-emacs@gnu.org>
0 siblings, 2 replies; 14+ messages in thread
From: Decebal @ 2009-04-12 7:25 UTC (permalink / raw)
To: help-gnu-emacs
On Apr 11, 10:08 am, thierry.volpia...@gmail.com wrote:
> > 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?
>
> Maybe that help?
> ,----
> | 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.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Something like an array (list) of a class
2009-04-12 7:25 ` Decebal
@ 2009-04-12 8:03 ` thierry.volpiatto
2009-04-12 16:15 ` Drew Adams
[not found] ` <mailman.5186.1239523825.31690.help-gnu-emacs@gnu.org>
1 sibling, 1 reply; 14+ messages in thread
From: thierry.volpiatto @ 2009-04-12 8:03 UTC (permalink / raw)
To: help-gnu-emacs
Decebal <CLDWesterhof@gmail.com> writes:
> On Apr 11, 10:08 am, thierry.volpia...@gmail.com wrote:
>> > 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?
>>
>> Maybe that help?
>> ,----
>> | 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"
`----
--
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France
^ permalink raw reply [flat|nested] 14+ messages in thread
* RE: Something like an array (list) of a class
2009-04-12 8:03 ` thierry.volpiatto
@ 2009-04-12 16:15 ` Drew Adams
2009-04-12 17:15 ` thierry.volpiatto
0 siblings, 1 reply; 14+ messages in thread
From: Drew Adams @ 2009-04-12 16:15 UTC (permalink / raw)
To: thierry.volpiatto, help-gnu-emacs
> >> > 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.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Something like an array (list) of a class
2009-04-12 16:15 ` Drew Adams
@ 2009-04-12 17:15 ` thierry.volpiatto
2009-04-12 17:45 ` Drew Adams
0 siblings, 1 reply; 14+ messages in thread
From: thierry.volpiatto @ 2009-04-12 17:15 UTC (permalink / raw)
To: Drew Adams; +Cc: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 14+ messages in thread
* RE: Something like an array (list) of a class
2009-04-12 17:15 ` thierry.volpiatto
@ 2009-04-12 17:45 ` Drew Adams
0 siblings, 0 replies; 14+ messages in thread
From: Drew Adams @ 2009-04-12 17:45 UTC (permalink / raw)
To: thierry.volpiatto; +Cc: help-gnu-emacs
> > 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 ?
I haven't tried to implement data-driven or object-oriented programming in Emacs
Lisp. I do take advantage of Customize type definition and type-checking, but so
far only in the context of real user options. And I do use some options that
users are not expected to modify using the Customize UI (rather, they use
particular commands that I provide, to do that).
In general, the Customize UI is one thing, and the Customize infrastructure is
another. I suspect that many Emacs-Lisp programmers, disliking the UI, miss
taking advantage of its variable defining and modifying infrastructure.
From my point of view, I don't care whether a user uses the UI or `setq' in an
init file. I use `defcustom' largely for its detailed type-defining features,
which are not bad. But so far I've done so only for real user options.
A few of the `defcustom' definitions in Icicles might be of interest:
http://www.emacswiki.org/emacs/icicles-opt.el. But I don't claim to be an expert
on Customize definitions. I'm sure you can find other, and more interesting,
`defcustom' definitions as food for thought. One place to look is the Emacs
source files.
^ permalink raw reply [flat|nested] 14+ messages in thread
[parent not found: <mailman.5186.1239523825.31690.help-gnu-emacs@gnu.org>]
* Re: Something like an array (list) of a class
[not found] ` <mailman.5186.1239523825.31690.help-gnu-emacs@gnu.org>
@ 2009-04-13 8:53 ` Decebal
2009-04-13 11:55 ` Decebal
0 siblings, 1 reply; 14+ messages in thread
From: Decebal @ 2009-04-13 8:53 UTC (permalink / raw)
To: help-gnu-emacs
On Apr 12, 10:03 am, thierry.volpia...@gmail.com wrote:
> There is no class in elisp but you have defstruct (be sure to require 'cl)
Looks like it that it does what I want. I want only the last field
mutable and the other fields parameterized. That is possible if I
understand it correctly.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Something like an array (list) of a class
2009-04-13 8:53 ` Decebal
@ 2009-04-13 11:55 ` Decebal
2009-04-13 12:27 ` Decebal
2009-04-13 14:56 ` Decebal
0 siblings, 2 replies; 14+ messages in thread
From: Decebal @ 2009-04-13 11:55 UTC (permalink / raw)
To: help-gnu-emacs
On 13 apr, 10:53, Decebal <CLDWester...@gmail.com> wrote:
> On Apr 12, 10:03 am, thierry.volpia...@gmail.com wrote:
>
> > There is no class in elisp but you have defstruct (be sure to require 'cl)
>
> Looks like it that it does what I want. I want only the last field
> mutable and the other fields parameterized. That is possible if I
> understand it correctly.
I tried the following:
(require 'cl)
(defstruct
(ModeLine
(:constructor nil)
(:constructor new-ModeLine (type description display function))
)
(type :read-only t)
(description :read-only t)
(display :read-only t)
(function :read-only t)
)
(setq e (new-ModeLine
"word"
"Display number of words"
"W"
'buffer-count-words
)
)
This seems to do what I want.
A few questions remain:
- Is there a way to make sure that type, description and display are
of type string and function of type function?
- Is there a way to make sure that none of the elements are empty?
- I would like to have a list (or other data structure) filled with
only objects of type ModeLine. What is the best way to do this?
- Is it possible to have all type and display values distinct?
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Something like an array (list) of a class
2009-04-13 11:55 ` Decebal
@ 2009-04-13 12:27 ` Decebal
2009-04-13 14:56 ` Decebal
1 sibling, 0 replies; 14+ messages in thread
From: Decebal @ 2009-04-13 12:27 UTC (permalink / raw)
To: help-gnu-emacs
On 13 apr, 13:55, Decebal <CLDWester...@gmail.com> wrote:
> (ModeLine
> (:constructor nil)
> (:constructor new-ModeLine (type description display function))
> )
> (type :read-only t)
> (description :read-only t)
> (display :read-only t)
> (function :read-only t)
> )
>
> (setq e (new-ModeLine
> "word"
> "Display number of words"
> "W"
> 'buffer-count-words
> )
> )
The function buffer-count-words exist, but how do I execute it? (eval
(ModeLine-function e)) doesnot work.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Something like an array (list) of a class
2009-04-13 11:55 ` Decebal
2009-04-13 12:27 ` Decebal
@ 2009-04-13 14:56 ` Decebal
2009-04-13 17:57 ` Decebal
1 sibling, 1 reply; 14+ messages in thread
From: Decebal @ 2009-04-13 14:56 UTC (permalink / raw)
To: help-gnu-emacs
On 13 apr, 13:55, Decebal <CLDWester...@gmail.com> wrote:
> I tried the following:
> (require 'cl)
>
> (defstruct
> (ModeLine
> (:constructor nil)
> (:constructor new-ModeLine (type description display function))
> )
> (type :read-only t)
> (description :read-only t)
> (display :read-only t)
> (function :read-only t)
> )
>
> (setq e (new-ModeLine
> "word"
> "Display number of words"
> "W"
> 'buffer-count-words
> )
> )
This does not do what I would expect. I can execute:
(setf (ModeLine-type e) "dummy")
And type word is changed to dummy. What am I doing wrong?
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Something like an array (list) of a class
2009-04-13 14:56 ` Decebal
@ 2009-04-13 17:57 ` Decebal
2009-04-14 0:52 ` Barry Margolin
0 siblings, 1 reply; 14+ messages in thread
From: Decebal @ 2009-04-13 17:57 UTC (permalink / raw)
To: help-gnu-emacs
I know have:
(require 'cl)
(defstruct
(ModeLine
(:constructor nil)
(:constructor new-ModeLine (type description display function))
)
(type :read-only t)
(description :read-only t)
(display :read-only t)
(function :read-only t)
)
(setq modelineArray
[
(new-ModeLine
"chars"
"Display number of chars"
"C"
'buffer-count-chars2
)
(new-ModeLine
"lines"
"Display number of lines"
"L"
'buffer-count-lines2
)
(new-ModeLine
"words"
"Display number of words"
"W"
'buffer-count-words2
)
]
)
But there are a few problems:
- I would like to have the vector modelineArray readonly. Is this
possible?
- The vector is notfilled with ModeLine objects. When executing:
(aref modelineArray 2)
I get:
(new-ModeLine "words" "Display number of words" "W" (quote buffer-
count-words2))
So I can not do something like:
(ModeLine-type (aref modelineArray 2))
What is the good way to fill the vector?
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Something like an array (list) of a class
2009-04-13 17:57 ` Decebal
@ 2009-04-14 0:52 ` Barry Margolin
2009-04-14 4:44 ` Decebal
0 siblings, 1 reply; 14+ messages in thread
From: Barry Margolin @ 2009-04-14 0:52 UTC (permalink / raw)
To: help-gnu-emacs
In article
<67e696b0-bdf8-45e2-86d6-4a696bb2ecfa@r37g2000yqn.googlegroups.com>,
Decebal <CLDWesterhof@gmail.com> wrote:
> I know have:
> (require 'cl)
>
> (defstruct
> (ModeLine
> (:constructor nil)
> (:constructor new-ModeLine (type description display function))
> )
> (type :read-only t)
> (description :read-only t)
> (display :read-only t)
> (function :read-only t)
> )
>
> (setq modelineArray
> [
> (new-ModeLine
> "chars"
> "Display number of chars"
> "C"
> 'buffer-count-chars2
> )
> (new-ModeLine
> "lines"
> "Display number of lines"
> "L"
> 'buffer-count-lines2
> )
> (new-ModeLine
> "words"
> "Display number of words"
> "W"
> 'buffer-count-words2
> )
> ]
> )
>
> But there are a few problems:
> - I would like to have the vector modelineArray readonly. Is this
> possible?
The syntax of a slot specification is
(slot-name default options...)
In your defstruct, you specified :read-only as the default, so it's not
being taken as an option name. Try:
(type nil :read-only t)
> - The vector is notfilled with ModeLine objects. When executing:
> (aref modelineArray 2)
> I get:
> (new-ModeLine "words" "Display number of words" "W" (quote buffer-
> count-words2))
> So I can not do something like:
> (ModeLine-type (aref modelineArray 2))
> What is the good way to fill the vector?
[...] is for literals, it doesn't evaluate its contents. Use (vector
...) instead.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Something like an array (list) of a class
2009-04-14 0:52 ` Barry Margolin
@ 2009-04-14 4:44 ` Decebal
0 siblings, 0 replies; 14+ messages in thread
From: Decebal @ 2009-04-14 4:44 UTC (permalink / raw)
To: help-gnu-emacs
On 14 apr, 02:52, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article
> <67e696b0-bdf8-45e2-86d6-4a696bb2e...@r37g2000yqn.googlegroups.com>,
> > But there are a few problems:
> > - I would like to have the vector modelineArray readonly. Is this
> > possible?
Is this possible, or do I want to much?
> The syntax of a slot specification is
>
> (slot-name default options...)
>
> In your defstruct, you specified :read-only as the default, so it's not
> being taken as an option name. Try:
>
> (type nil :read-only t)
That works.
> > - The vector is notfilled with ModeLine objects. When executing:
> > (aref modelineArray 2)
> > I get:
> > (new-ModeLine "words" "Display number of words" "W" (quote buffer-
> > count-words2))
> > So I can not do something like:
> > (ModeLine-type (aref modelineArray 2))
> > What is the good way to fill the vector?
>
> [...] is for literals, it doesn't evaluate its contents. Use (vector
> ...) instead.
That works also. I am slowly getting there.
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2009-04-14 4:44 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-04-10 20:32 Something like an array (list) of a class Decebal
2009-04-11 8:08 ` thierry.volpiatto
[not found] ` <mailman.5118.1239437738.31690.help-gnu-emacs@gnu.org>
2009-04-12 7:25 ` Decebal
2009-04-12 8:03 ` thierry.volpiatto
2009-04-12 16:15 ` Drew Adams
2009-04-12 17:15 ` thierry.volpiatto
2009-04-12 17:45 ` Drew Adams
[not found] ` <mailman.5186.1239523825.31690.help-gnu-emacs@gnu.org>
2009-04-13 8:53 ` Decebal
2009-04-13 11:55 ` Decebal
2009-04-13 12:27 ` Decebal
2009-04-13 14:56 ` Decebal
2009-04-13 17:57 ` Decebal
2009-04-14 0:52 ` Barry Margolin
2009-04-14 4:44 ` Decebal
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).