clos

How to force slot's type to be checked during make-instance?

限于喜欢 提交于 2019-12-04 07:28:56
Let's say I have the following class declaration: (defclass foo-class () ((bar :initarg :bar :type list))) When I create an instance of this class, make-instance won't check whether passed arguments satisfy types of slots. So, I can create "invalid" objects this way: > (make-instance 'foo-class :bar 'some-symb) #<FOO-CLASS {102BEC5E83}> However, what I'd like to see is the behavior similar to the creation of an instance of a struct, where the types are checked: (defstruct foo-struct (bar nil :type list)) > (make-foo-struct :bar 'some-symb) ;; raises contition: ;; ;; The value ;; SOME-SYMB ;;

Saving CLOS objects

痴心易碎 提交于 2019-12-02 03:59:59
问题 Saving any Common Lisp structure object to a file (readably) seems relatively straightforward with something like (defun save-structure-object (object filename) (with-open-file (stream filename :direction :output :if-exists :supersede) (with-standard-io-syntax (print object stream)))) For a CLOS object instance, however, the post at Make clos objects printable in lisp indicates a more complex recipe. First, does the comment about closer-mop relate to a simpler approach to saving a clos class

Is there a way to gather slot-definition-readers from all the inheritance tree?

此生再无相见时 提交于 2019-12-02 02:52:17
The generic function slot-definition-readers gets an argument that must be a direct-slot-definition . If an object is an instance of a class that inherits from another class how can I get hold of the readers of all the effective-slots of the object? Do I manually have to traverse the tree and call slot-definition-readers on the result of class-direct-slots in each superclass, gathering the results, or is there another way that I am not aware of? coredump This "community wiki" answer is here to provide an implementation of this feature. What follows uses no destructive operation (NCONC, MAPCAN)

Saving CLOS objects

狂风中的少年 提交于 2019-12-01 22:50:48
Saving any Common Lisp structure object to a file (readably) seems relatively straightforward with something like (defun save-structure-object (object filename) (with-open-file (stream filename :direction :output :if-exists :supersede) (with-standard-io-syntax (print object stream)))) For a CLOS object instance, however, the post at Make clos objects printable in lisp indicates a more complex recipe. First, does the comment about closer-mop relate to a simpler approach to saving a clos class instance? And second, is the code offered there presented as a general utility for printing any clos

Comprehensive guide on common lisp types

一笑奈何 提交于 2019-12-01 21:29:55
问题 Maybe this question is too general, nevertheless i'll try: Is there any comprehensive guide on types in common lisp? I'm kind of confused about this subject: Why are non-primitive types declared in make-array 's :element-type are promoted to t ? Is there any possibility for compile-time or runtime checks of the real declared type? Why are CLOS slot defined types don't work as constraints, allowing to put value of any type into the slot? Again, what about the checks? The same for the functions

memory usage by objects in common lisp

六月ゝ 毕业季﹏ 提交于 2019-12-01 17:29:16
Is there a way to find out how much memory is used by an instance of a class or basic data types in general? I have a toy webframework in cl that creates and manages web pages with instances of classes that represent the html tags and their properties, and as they are supposed to make an html page, they have children in a slot called children. so I was thinking how much a user's session will cost the server if I take this approach. Thanks. As far as I know, there is nothing like this for arbitrary objects in the standard, but there are implementation-dependent solutions, like ccl:object-direct

common lisp: slot-value for defstruct structures

孤街浪徒 提交于 2019-12-01 16:21:50
In common lisp, what can I use to access structure slot using slot name/symbol? What I want is (defstruct point (x 0) (y 0)) (defmacro -> (struct slot) `(slot-value ,struct ,slot)) (setf p (make-point)) (setf (slot-value p 'x) 1) (setf (-> p 'y) 2) I'm using clozure cl, and In clozure cl this works. However, AFAIK this is non-standard behavior (equivalent to "undefined behavior" C++). I'm not planning to switch to another CL implementation, so should I keep using slot-value for structures, or is there a better way to do it? Usually you would use accessor functions with structures. Your code

Is there a way to get the slots of a class?

我的未来我决定 提交于 2019-12-01 15:20:46
问题 I have a class like this one (defclass shape () ((color :initform :black) (thickness :initform 1) (filledp :initform nil) (window :initform nil))) Is there a function in common-lisp how to get a list of those slots if i only know instance of this class? 回答1: Many Common Lisp implementations support the CLOS Meta-object Protocol . This provides introspective operations for classes, slots and other meta objects . In LispWorks the corresponding functions are directly accessible in the package CL

common lisp: slot-value for defstruct structures

孤者浪人 提交于 2019-12-01 15:03:55
问题 In common lisp, what can I use to access structure slot using slot name/symbol? What I want is (defstruct point (x 0) (y 0)) (defmacro -> (struct slot) `(slot-value ,struct ,slot)) (setf p (make-point)) (setf (slot-value p 'x) 1) (setf (-> p 'y) 2) I'm using clozure cl, and In clozure cl this works. However, AFAIK this is non-standard behavior (equivalent to "undefined behavior" C++). I'm not planning to switch to another CL implementation, so should I keep using slot-value for structures, or

Is there a generic method for cloning CLOS objects?

五迷三道 提交于 2019-11-29 07:03:43
I'm looking for a way to clone CLOS objects in a shallow manner, so the created object would be of the same type with the same values in each slot, but a new instance. The closest thing I found is a standard function copy-structure which does this for structures. There is no standard predefined way to copy CLOS objects in general. It is not trivial, if possible at all, to provide a reasonable default copy operation that does the right thing (at least) most of the time for arbitrary objects, since the correct semantics change from class to class and from application to application. The extended