Lisp HUG Maillist Archive

CLOS slot and :type

I'm wondering what the :type in a CLOS object slot is actually used for, and if my using it for something else is going to end up causing a world of problems?

https://github.com/massung/json

My JSON package for LW has  a very helpful function: json-decode-into. You basically provide a class symbol and away it goes. For example:

(defclass login ()
  ((|username| :reader login-username)
   (|password| :reader login-password)))

And now if you had the following JSON string:

{ "username" : "jeff", "password" : "12345" }

Then:

(json-decode-into 'login json-string) ;=> #<LOGIN>

Nothing terribly special here. However, often times there will be objects within objects. And, so I allow for a particular slot to use :type to identify when the JSON decoder should recurse:

(defclass account ()
  ((|login|   :reader account-login :type login)
   (|balance| :reader account-balance)))

With the above, calling (json-decode-into 'account ...), if a "login" key is found, it will recursively call (json-decode-into 'login ...) since that's the :type of the slot (and a subtype of standard-object).

I'm just curious if that - by doing this - I'm circumventing something else either in the standard or the LW implementation of CLOS?

Jeff M.

Re: CLOS slot and :type

Hello Jeff,

I think that is a great use of the TYPE slot option. I had to look at your code to see exactly how you were using the information and now I, too, want to start taking advantage of MOP:SLOT-DEFINITION-TYPE. In the past, I've only used the TYPE slot option as a form of documentation and have never expected or relied on it to do anything useful because of the following statement.

The consequences of attempting to store in a slot a value that does not satisfy the type of the slot are undefined.

Although, I ran into a TYPE slot issue once with CCL because it actually verifies that the value for the slot is compatible with the type of the slot. The same code ran without error or warning on LW.

Good luck,

Tom H.

----------------------------------------------------------------
Thomas M. Hermann
Odonata Research LLC
316-285-0120
http://www.odonata-research.com/
http://www.linkedin.com/in/thomasmhermann


On Tue, Sep 24, 2013 at 1:50 PM, Jeffrey Massung <massung@gmail.com> wrote:
I'm wondering what the :type in a CLOS object slot is actually used for, and if my using it for something else is going to end up causing a world of problems?

https://github.com/massung/json

My JSON package for LW has  a very helpful function: json-decode-into. You basically provide a class symbol and away it goes. For example:

(defclass login ()
  ((|username| :reader login-username)
   (|password| :reader login-password)))

And now if you had the following JSON string:

{ "username" : "jeff", "password" : "12345" }

Then:

(json-decode-into 'login json-string) ;=> #<LOGIN>

Nothing terribly special here. However, often times there will be objects within objects. And, so I allow for a particular slot to use :type to identify when the JSON decoder should recurse:

(defclass account ()
  ((|login|   :reader account-login :type login)
   (|balance| :reader account-balance)))

With the above, calling (json-decode-into 'account ...), if a "login" key is found, it will recursively call (json-decode-into 'login ...) since that's the :type of the slot (and a subtype of standard-object).

I'm just curious if that - by doing this - I'm circumventing something else either in the standard or the LW implementation of CLOS?

Jeff M.

Re: CLOS slot and :type

On Tue, 24 Sep 2013 19:50:55 +0100, Jeffrey Massung <massung@gmail.com>  
wrote:

> I'm wondering what the :type in a CLOS object slot is actually used for,  
> and if my using it for something else is going to end up causing a world  
> of problems?

:type is a specifier used by defstruct to define data types of its slots,  
so I'd imagine defclass would have inherited it for the same purpose.

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Re: CLOS slot and :type

* Jeffrey Massung <5241DF0F.5060109@gmail.com> :
Wrote on Tue, 24 Sep 2013 13:50:55 -0500:

| I'm wondering what the :type in a CLOS object slot is actually used
| for, and if my using it for something else is going to end up causing
| a world of problems?
|
| (defclass login ()
| ((|username| :reader login-username)
| (|password| :reader login-password)))

I believe CL-SQL uses the :type slot in DEF-VIEW-CLASS to map the lisp
type to the DB-TYPE.  I've used the CLOS slot-definition-types, for
eg. to automatically build up CAPI interfaces for editing displaying the
classes.  I only ever did this with LW.  There were a few drawbacks [off
the top of my head from maybe 5 years ago]---Madhu

1. I had to resort to a naming convention to distinguish slots that
   represented JOIN-CLASSes, naming them as SLOTNAME-OBJ, so I could
   skip them or handle them specially when operating on slot names (the
   DB-COLUMNS) and type names.

2. I could not get at the slot :documentation strings, and so had to
   resort to annotating the view-classes separately, and use this
   information when operating on the slot names and types names.

3. Differences in translating the lisp types to various database
   backends had to be kluged in a few cases, this was inherently
   fragile.

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Re: CLOS slot and :type

This may not be LW specific, but you may wish to have a look at Alexander Repenning's XMLisp package, which spends a bit of its effort on this issue of automatically constructing/aggregating objects based on slot definition type.  Some of the techniques (eg determining plurality of slot value based on English plurality of slot name; integration of xml decoding with lisp reader using macro character) you probably want to avoid, but perhaps some others might provide food for thought. 

Regards,
Dan

Sent from my iPad

On Sep 24, 2013, at 2:50 PM, Jeffrey Massung <massung@gmail.com&g t; wrote:

I'm wondering what the :type in a CLOS object slot is actually used for, and if my using it for something else is going to end up causing a world of problems?

https://github.com/massung/json

My JSON package for LW has  a very helpful function: json-decode-into. You basically provide a class symbol and away it goes. For example:

(defclass login ()
  ((|username| :reader login-username)
   (|password| :reader login-password)))

And now if you had the following JSON string:

{ "username" : "jeff", "password" : "12345" }

Then:

(json-decode-into 'login json-string) ;=> #<LOGIN>

Nothing terribly special here. However, often times there will be objects within objects. And, so I allow for a particular slot to use :type to identify when the JSON decoder should recurse:

(defclass account ()
  ((|login|   :reader account-login :type login)
   (|balance| :reader account-balance)))

With the above, calling (json-decode-into 'account ...), if a "login" key is found, it will recursively call (json-decode-into 'login ...) since that's the :type of the slot (and a subtype of standard-object).

I'm just curious if that - by doing this - I'm circumventing something else either in the standard or the LW implementation of CLOS?

Jeff M.
Updated at: 2020-12-10 08:35 UTC