Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to overload a method from a foreign package
    primarykey
    data
    text
    <p>Given the szenario that you got a base package which can represent certain stuff via a class and another package which wants to extend this ability.</p> <pre><code>(defpackage :test (:use :cl) (:nicknames :test) (:export a-test-class method-a slot-a)) (in-package :test) (defclass a-test-class () ((slot-a :initform 42 :reader slot-a))) (defmethod method-a ((a-test-class a-test-class)) (setf (slot-value a-test-class 'slot-a) 21) a-test-class) (defpackage :exttest (:use :cl) (:export extended-a-test-class method-a)) (in-package :exttest) (defclass extended-a-test-class (test:a-test-class) ((slot-b :reader slot-b :initform nil))) (defmethod method-a ((a-test-class extended-a-test-class)) (setf (slot-value a-test-class 'slot-a) 23) a-test-class) </code></pre> <p>Now I got a function which does not really do anthying but go over a list of instances of <code>a-test-class</code> and <code>extended-a-test-class</code> and is supposed to call <code>method-a</code> on all of them, expecting them to change, respectively to their type. E.g. <code>(slot-a (method-a a-test-class-instance)) &gt; 21</code> and <code>(slot-a (method-a extended-a-test-class-instance)) &gt; 23</code></p> <p>But trying to do this, I run into the problem of correctly calling method-a as:</p> <pre><code>(defparameter *test-instance* (make-instance 'test:a-test-class)) (defparameter *ext-test-instance* (make-instance 'exttest:extended-a-test-class)) (test:slot-a (test:method-a *test-instance*)) &gt; 21 (test:slot-a (test:method-a *ext-test-instance*)) &gt; 21 </code></pre> <p>or</p> <pre><code>(test:slot-a (exttest:method-a *test-instance*)) (test:slot-a (exttest:method-a *ext-test-instance*)) debugger invoked on a SIMPLE-ERROR in thread #&lt;THREAD "main thread" RUNNING {1002B03193}&gt;: There is no applicable method for the generic function #&lt;STANDARD-GENERIC-FUNCTION EXTTEST:METHOD-A (1)&gt; when called with arguments (#&lt;TEST:A-TEST-CLASS {10041148A3}&gt;) </code></pre> <p>Is neither really working for me, as in either way I am not able to compile, or the effect of the method is not as desired. If the classes and method definitions are in the same package, everything works fine though.</p> <p>Therefore: <strong>How can I call a method on an instance without needing to address the corresponding package?</strong> <em>(If I am not able to do so, I'd like to know how my expectations for OO-Programming in Common-Lisp are misguided)</em></p> <p>For an "working" example, of what output I'd like, I coded this c++ program. I do known that CLOS works different from the "common" Object Oriented System, due to the fact that methods do not "belong" to classes. But I'd expect any object oriented system to (somehow) be able to behave/be used like this:</p> <pre><code>#include &lt;iostream&gt; namespace test { class sub { public: virtual sub* method_a() = 0; }; class a_test_class : public sub { protected: int value; public: a_test_class(int val) : value(val) { } a_test_class* method_a() { value = 21; return this; } int get_value() { return value; } }; } namespace exttest { class extended_a_test_class : public test::a_test_class { public: extended_a_test_class(int val) : a_test_class(val) { } extended_a_test_class* method_a() { std::cout &lt;&lt; "calling overloaded method" &lt;&lt; std::endl; this-&gt;value = 23; return this; } }; } int main(int argc,const char* argv[]) { test::a_test_class* atc = new test::a_test_class(42); test::a_test_class* eatc = new exttest::extended_a_test_class(42); std::cout &lt;&lt; atc-&gt;method_a()-&gt;get_value() &lt;&lt; std::endl; std::cout &lt;&lt; eatc-&gt;method_a()-&gt;get_value() &lt;&lt; std::endl; delete atc; delete eatc; } &gt; ./a.out 21 calling overloaded method 23 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload