Code: Select all
operator int () { return this->value; }
Thank you for any ideas, I may be doing it totally wrong, so don't hesitate to suggest a better way of doing the same thing!
Code: Select all
operator int () { return this->value; }
I don't understand why you need this (apart from dealing with some Java-ism). Remember that you can still dowvxvw wrote:Hello, I'm looking for basically to do this:I.e. I need an object, which, although not an integer, may be used as an integer in other situations by simply using one of it's slots (it's a date object, if you need to know). When I'm trying to extend from integer class it says that integer and standard-class are incompatible. Or, better say, it wants me to add SB-MOP:VALIDATE-SUPERCLASS method to "handle" it somehow, yet I don't really understand how am I supposed to handle it :SCode: Select all
operator int () { return this->value; }
Thank you for any ideas, I may be doing it totally wrong, so don't hesitate to suggest a better way of doing the same thing!
Code: Select all
(defmethod foo ((x integer)) ...)
Usually by using TYPECASE. This obviously requires all relevant types to be listed in the dispatching form, which is why type dispatch is usually not extensible.wvxvw wrote:As an aside, how would I use type defined in (deftype ...) to dispatch?
Code: Select all
;; amf-value-p is defined elsewhere,
;; amf-object is also defined elsewhere and is a class with a slot called 'properties
(deftype amf-value ()
'(satisfies amf-value-p))
;; I don't want to eventually set the value in the hash table
;; to something that I potentially won't be able to serialize later
;; so, amf-value is rather not very specific, but there are things that
;; this function won't accept, like, complex numbers for example
(defmethod set-property ((container amf-object) (name string) value)
(declare (type amf-value value))
(setf (gethash name (slot-value container 'properties)) value))
If you want to guarantee that, you'd be better to use (assert (amf-value-p value)) rather than the type declaration, which serves no purpose here (CMUCL turns it into a check, under some compiler settings, but other implementations won't).wvxvw wrote:Thank you, but I rather meant something different. I'd like, on the contrary, to help to narrow down the possible selection options. I.e. here's an example:
Code: Select all
;; I don't want to eventually set the value in the hash table ;; to something that I potentially won't be able to serialize later ;; so, amf-value is rather not very specific, but there are things that ;; this function won't accept, like, complex numbers for example (defmethod set-property ((container amf-object) (name string) value) (declare (type amf-value value)) (setf (gethash name (slot-value container 'properties)) value))
Code: Select all
(defmethod (setf symbol) (...))
That is just a method version of defining a SETF function. It is explained in chapter 17 of Practical Common Lisp. In short, a list of two elements like (setf name) is a valid function name, and SETF forms can expand to it as described by 5.1.2.9.wvxvw wrote:How would this work?
Well, there is something called filtered-dispatch, which allows dispatching on arbitrary predicates, but I am not sure how widely it is used, if at all. And in a narrow case of just type checking I don't see what it would add to inserting the check directly into the method, or maybe defining a :before method.wvxvw wrote:I was going to ask if one could possibly modify / extend method (and defmethod macro) to allow using types as constrains for arguments