From: ············@gmail.com
Subject: array initialisation
Date: 
Message-ID: <51a6e6a4-50d3-4644-9a0d-a489297a6617@a1g2000hsb.googlegroups.com>
Suppose I define a class foo
CL-USER> (defclass foo () ())
#<STANDARD-CLASS FOO>

Now I define a function that creates an array of instances of class
'foo.
CL-USER> (defun bar (x)
                     (make-array x :element-type 'foo :initial-element
nil))
; in: LAMBDA NIL
;     (MAKE-ARRAY X :ELEMENT-TYPE 'FOO :INITIAL-ELEMENT NIL)
;
; caught STYLE-WARNING:
;   NIL is not a FOO.
;
; compilation unit finished
;   caught 1 STYLE-WARNING condition
BAR

I get a warning (that I could ignore) since NIL is indeed not an
instance of the FOO class.
How to create such an array with an initial value that allows me to
test whether one of its element has already been instanciated or not ?


-Nicolas

From: Thomas A. Russ
Subject: Re: array initialisation
Date: 
Message-ID: <ymi7iege5ig.fsf@blackcat.isi.edu>
············@gmail.com writes:

> Suppose I define a class foo
> CL-USER> (defclass foo () ())
> #<STANDARD-CLASS FOO>
> 
> Now I define a function that creates an array of instances of class
> 'foo.
> CL-USER> (defun bar (x)
>                      (make-array x :element-type 'foo :initial-element
> nil))
...
> I get a warning (that I could ignore) since NIL is indeed not an
> instance of the FOO class.
> How to create such an array with an initial value that allows me to
> test whether one of its element has already been instanciated or not ?

Well, there's always:

(defconstant +NULL-FOO+ (make-instance 'foo))

(defun bar (x)
  (make-array x :element-type 'foo :initial-element +NULL-FOO+))

(eq (aref (bar 10) 3) +NULL-FOO+)



-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Edi Weitz
Subject: Re: array initialisation
Date: 
Message-ID: <u3ap4kdlp.fsf@agharta.de>
On Tue, 29 Apr 2008 05:36:44 -0700 (PDT), ············@gmail.com wrote:

> Suppose I define a class foo
> CL-USER> (defclass foo () ())
> #<STANDARD-CLASS FOO>
>
> Now I define a function that creates an array of instances of class
> 'foo.
> CL-USER> (defun bar (x)
>                      (make-array x :element-type 'foo :initial-element
> nil))
> ; in: LAMBDA NIL
> ;     (MAKE-ARRAY X :ELEMENT-TYPE 'FOO :INITIAL-ELEMENT NIL)
> ;
> ; caught STYLE-WARNING:
> ;   NIL is not a FOO.
> ;
> ; compilation unit finished
> ;   caught 1 STYLE-WARNING condition
> BAR
>
> I get a warning (that I could ignore) since NIL is indeed not an
> instance of the FOO class.  How to create such an array with an
> initial value that allows me to test whether one of its element has
> already been instanciated or not ?

How about '(or null foo) as the element type?

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: ············@gmail.com
Subject: Re: array initialisation
Date: 
Message-ID: <0804b5f9-95d0-4122-ba28-66609a15e090@k37g2000hsf.googlegroups.com>
On Apr 29, 3:14 pm, Edi Weitz <········@agharta.de> wrote:
> On Tue, 29 Apr 2008 05:36:44 -0700 (PDT), ············@gmail.com wrote:
> > Suppose I define a class foo
> > CL-USER> (defclass foo () ())
> > #<STANDARD-CLASS FOO>
>
> > Now I define a function that creates an array of instances of class
> > 'foo.
> > CL-USER> (defun bar (x)
> >                      (make-array x :element-type 'foo :initial-element
> > nil))
> > ; in: LAMBDA NIL
> > ;     (MAKE-ARRAY X :ELEMENT-TYPE 'FOO :INITIAL-ELEMENT NIL)
> > ;
> > ; caught STYLE-WARNING:
> > ;   NIL is not a FOO.
> > ;
> > ; compilation unit finished
> > ;   caught 1 STYLE-WARNING condition
> > BAR
>
> > I get a warning (that I could ignore) since NIL is indeed not an
> > instance of the FOO class.  How to create such an array with an
> > initial value that allows me to test whether one of its element has
> > already been instanciated or not ?
>
> How about '(or null foo) as the element type?
>

Beside the fact this works, this is elegant ;)
The more I learn Lisp, the more I find it consistent and powerfull.
Thank you.

-Nicolas
From: Pascal J. Bourguignon
Subject: Re: array initialisation
Date: 
Message-ID: <7cd4o73hcm.fsf@pbourguignon.anevia.com>
············@gmail.com writes:

> Suppose I define a class foo
> CL-USER> (defclass foo () ())
> #<STANDARD-CLASS FOO>
>
> Now I define a function that creates an array of instances of class
> 'foo.
> CL-USER> (defun bar (x)
>                      (make-array x :element-type 'foo :initial-element
> nil))
> ; in: LAMBDA NIL
> ;     (MAKE-ARRAY X :ELEMENT-TYPE 'FOO :INITIAL-ELEMENT NIL)
> ;
> ; caught STYLE-WARNING:
> ;   NIL is not a FOO.
> ;
> ; compilation unit finished
> ;   caught 1 STYLE-WARNING condition
> BAR
>
> I get a warning (that I could ignore) since NIL is indeed not an
> instance of the FOO class.
> How to create such an array with an initial value that allows me to
> test whether one of its element has already been instanciated or not ?

Like Thomas says, or:

(defun bar (x)
   (make-array x :element-type '(or null foo) :initial-element  nil))

which should not make much difference when foo is not a 'basic' type
like fixnum, character or double-float...

-- 
__Pascal Bourguignon__
From: Steven M. Haflich
Subject: Re: array initialisation
Date: 
Message-ID: <481A9839.9020107@alum.mit.edu>
> Like Thomas says, or:
> 
> (defun bar (x)
>    (make-array x :element-type '(or null foo) :initial-element  nil))
> 
> which should not make much difference when foo is not a 'basic' type
> like fixnum, character or double-float...

I don't think the logic of this statement is necessarily quite correct.
It depends on how you interpret the ANS specifications near the end of 
this message.

First ask the question why you are declaring the type of this slot.
If it is only for documentation, then it could be replaced by actual
documentation.  But if it is on the assumption that the declaration
will result in some efficiency gain, that assump0tion needs examination.

In Java a type declaration of some Class type frammis is effectively
equivalent to the CL declaration (or null frammis).  Whether this is
a good or bad language design is a subject for a different thread.
For CL, the question should be what use is this declaration.

There may be some circumstances in some implementations where a type
declaration on a slot may have actual effect in optimizing code.  (Most
typically, a compiler might be able to reason about the type returned
by a slot reader.)  It is somewhat less likely that a given
implementation will be able to use a declaration (or null frammis).
This is despite the fact that any operation (e.g. slot-value) done on a
frammis would not be legal done on nil, so the very fact that such an
operation is coded is a pro forma declaration that the object is of a
reasonable, non-erring type.  You'd need to experiment with your
implementation of choice to find out.

The ANS is schizophrenic about whether the compiler can do anything with
a class declaration.  The problem is that classes can be redefined, and
the mapping between names and classes can be changed at run time.  This 
is taken from ANS 3.2.2.3:

   Type definitions made with deftype or defstruct in the compilation
   environment must retain the same definition at run time. Classes
   defined by defclass in the compilation environment must be defined at
   run time to have the same superclasses and same metaclass.

What does "same: mean in this context?  it is defined as a term of art
in the ANS as being approximately the same as satisfying EQL, but
redefining a class does not violate eq-ness of the class object.  Nor
does a redefinition of its metaclass, which imponderable grotesqueness
is not prohibited by the ANS, nor (I think) by the MOP.  If the compiler
must allow for these possibilities at future run time, there isn't a lot
it can optimize, unless it implements very clever memoization of the
class structure.

It is hard to think about thinking about these things.  Flame on, Macduff.
From: Pascal J. Bourguignon
Subject: Re: array initialisation
Date: 
Message-ID: <7ctzhgzqvs.fsf@pbourguignon.anevia.com>
"Steven M. Haflich" <···@alum.mit.edu> writes:

>> Like Thomas says, or:
>> (defun bar (x)
>>    (make-array x :element-type '(or null foo) :initial-element  nil))
>> which should not make much difference when foo is not a 'basic' type
>> like fixnum, character or double-float...
>
> I don't think the logic of this statement is necessarily quite correct.
> It depends on how you interpret the ANS specifications near the end of
> this message.
>
> First ask the question why you are declaring the type of this slot.
> If it is only for documentation, then it could be replaced by actual
> documentation.  But if it is on the assumption that the declaration
> will result in some efficiency gain, that assump0tion needs examination.

Well, I meant specifically that once you write (or null foo), the
array will be upgraded to T, and you'll get a simple-array instead of
an array of a specific element type.  And I meant that in the case of
non basic types, the array will anyways be upgraded to T (that is, the
slots will contain references, not values), indeed because in the case
of CLOS objects they may change of class and layout.  

For structures, it's implementation specific, and we can envision an
implementation that would store structures values in a specialized
array.   So I should have added "possibly structures" to my list of
'basic' types. (And perhaps also some other type of lisp objects, such
as random states, readtables, etc).

But I guess most implementations will use references for anything
that's not scalar.

-- 
__Pascal Bourguignon__