Re: defining methods based on built-in ELisp types

Jesse Alama <[email protected]> Fri, 12 Oct 2012 11:26:41 +0200
Newsgroups gmane.emacs.eieio
Message-ID <[email protected]>
"Eric M. Ludlam" <[email protected]> writes:

> On 10/10/2012 07:48 AM, Jesse Alama wrote:
>> I recently started to use EIEIO in my ELisp programming.  Coming from
>> CLOS, I naturally started to define methods like
>>
>>    (defmethod foo ((x string) ...))
>>
>>    (defmethod foo ((x cons)) ...)
>>
>> But this seems to be unsupported.  It looks like this lack of
>> functionality is behind item #2 on the EIEIO wish list ("Method
>> dispatch for built-in types").  Has there been any progress on this
>> front?
>
> As far as I know, no one has looked into how to make that work.  Most of 
> the projects I've worked on have only needed new classes, not extensions 
> of built-in types.
>
> I suspect any such feature would need to be done from Emacs core.   I 
> suppose it could be faked from the outside, but there would be no useful 
> features to inherit from the base type since they have no methods. ;)

I figured that this would probably involve some low-level hacking.
The typical use case that I have in mind is where I define methods
based on a the (primitive) type of the first argument to help clean up
my code.  I find that the three methods

  (defgeneric foo (x))
  (defmethod foo ((x cons)) ...)
  (defmethod foo ((x string)) ...)
  (defmethod foo ((x sequence)) ...)

is more elegant and maintainable, and promotes greater type safety,
more clearly than

  (defun foo (x)
    (cond ((consp x) ...)
          ((stringp x) ...)
          ((sequencep x) ...)
          (t (error "Don't know how to handle '%s'" x))))

or

  (defun foo-cons (x) (...))
  (defun foo-string (x) (...))
  (defun foo-sequence (x) (...))
  (defun foo (x)
    (cond ((consp x) (foo-cons x))
          ...))

>From this perspective, the lack of methods for objects belong to the
base system classes (or types) isn't a problem.

Jesse

-- 
Jesse Alama


------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev