brpocock / incongruent-methods Goto Github PK
View Code? Open in Web Editor NEWThis project forked from pve1/incongruent-methods
Methods with incongruent lambda lists.
License: Other
This project forked from pve1/incongruent-methods
Methods with incongruent lambda lists.
License: Other
* Incongruent methods ** Basics Incongruent methods is a simple implementation of incongruent (overloaded) methods using standard CLOS methods. These methods do not support &key, &rest, &optional etc. Example: (define-incongruent-method hello ((me string)) (concatenate 'string "Hello, " me)) (define-incongruent-method hello ((me string) (greeting string)) (concatenate 'string greeting ", " me)) (hello "world") ==> "Hello world" (hello "world" "Hey") ==> "Hey world" Example 2: (defclass hello () ((message :initarg :message :initform nil :accessor %get-message))) ;; standard generic accessor. (define-incongruent-method greet ((me hello)) (print (%get-message me))) (define-incongruent-method greet ((me hello) (n integer)) (dotimes (m n) (greet me))) ** Shared methods Methods that are "shared" between packages can be also be defined. These methods can be called using the "imcall" function. It is advisable that shared methods are specialized on at least one class defined in your own system. A shared method specializing only on, say, the built-in integer class, might accidentally get redefined by other systems. - Operations that are part of the "public interface" of a class are good candidates for shared methods. - In the following example, notice how the call to "notify" in the do-something method refers to test-package-1::notify, not test-package-2::notify. In fact, test-package-1 has no knowledge of the other package and can be compiled separately if one wishes. Example: (defpackage :test-package-1 (:use :cl :incongruent-methods) (:export #:my-class)) (in-package :test-package-1) (defclass my-class () ((slot :accessor %get-slot))) (define-shared-method slot-contents ((me my-class)) (slot-value me 'slot)) (define-shared-method get-something ((me my-class)) 'something) (define-shared-method do-something ((me my-class) (object t)) (format t "I did something!~%") (imcall 'notify object :done)) ;;;; (defpackage :test-package-2 (:use :cl :incongruent-methods)) (in-package :test-package-2) (defclass some-random-class () ()) (define-shared-method notify ((me some-random-class) (status symbol)) (format t "Received status: ~A~%" status)) (defun main () (let ((my-object (make-instance 'test-package-1:my-class)) (some-object (make-instance 'some-random-class))) (format t "~S~%" (imcall 'get-something my-object)) (imcall 'do-something my-object some-object))) CL-USER> (main) ==> TEST-PACKAGE-1::SOMETHING I did something! Received status: DONE ** Define-class A convenience macro for defining classes is also provided. The symbol "ME" in the body of the methods means the current object (self, this etc). It should also be the first argument when calling methods defined using this macro. Accessors will be converted to incongruent methods. Example: (define-class greeting () ((message :accessor message :initform nil) (some-slot :accessor some-slot :initform nil)) ;; Methods can be defined here, if one wants. (greet () ;; lambda list is actually ((me greeting)) (message me)) (greet ((n integer)) ;; lambda list is actually ((me greeting) (n integer)) (dotimes (m n) (greet me))) (greet ((n (eql 42))) (some-slot me)) ((setf greet) (new) (setf (message me) new))) (let ((i (make-instance 'greeting :message "Hello"))) (greet i)) ==> "Hello" (let ((i (make-instance 'greeting :message "Hello"))) (setf (greet i) "Hi") (greet i)) ==> "Hi"
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.