* Colorize objects by method dispatch type
@ 2020-04-01 20:25 Michael Heerdegen
2020-04-01 21:46 ` Michael Heerdegen
2020-04-02 3:00 ` Stefan Monnier
0 siblings, 2 replies; 8+ messages in thread
From: Michael Heerdegen @ 2020-04-01 20:25 UTC (permalink / raw)
To: Emacs mailing list
Hi,
Question first: For `cl-defmethod's, is there a way to get a method
dispatch for a given generic name and a set of parameters, e.g. a
reference to the dispatched method?
The background:
On my phone, every contact automatically gets assigned a color. It's
random but stays always the same for a contact, and in the long run, I
get used to the assigned colors and this is very helpful.
I now want to teach Emacs to automatically colorize objects presented in
some buffer by types in a similar way. For example, I want to colorize
registers in the register preview by the type of their contents so that
registers containing stuff of the same type all get the same color. I
don't want to define the colors myself, at least not always.
What I currently do, and this already works very nicely, is to use the
result of `type-of' called on the registers, hash it with `secure-hash'
and use a part of the hash, which is hex code, as color definition.
But in the case of registers, the already existing method
implementations defined by the developers for the different types of
registers provide a better source for an equality relation on registers
than `type-of.' That's why I would rather use references to the
appropriate method implementations for hashing.
Thanks,
Michael.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Colorize objects by method dispatch type
2020-04-01 20:25 Colorize objects by method dispatch type Michael Heerdegen
@ 2020-04-01 21:46 ` Michael Heerdegen
2020-04-02 3:00 ` Stefan Monnier
1 sibling, 0 replies; 8+ messages in thread
From: Michael Heerdegen @ 2020-04-01 21:46 UTC (permalink / raw)
To: Emacs mailing list
Michael Heerdegen <michael_heerdegen@web.de> writes:
> That's why I would rather use references to the appropriate method
> implementations for hashing.
A nice addition would be to take ancestors into account, somehow.
E.g. if I have a class C_n, and I know it has ancestors
C_n <- C_n-1 <- ... <- C_1
then the color for C_n could be, instead of it's directly derived color
c_n, the arithmetical mean of c_n and the colors of the ancestors, so
that related classes get similar colors.
Michael.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Colorize objects by method dispatch type
2020-04-01 20:25 Colorize objects by method dispatch type Michael Heerdegen
2020-04-01 21:46 ` Michael Heerdegen
@ 2020-04-02 3:00 ` Stefan Monnier
2020-04-02 3:45 ` Michael Heerdegen
1 sibling, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2020-04-02 3:00 UTC (permalink / raw)
To: help-gnu-emacs
> But in the case of registers, the already existing method
> implementations defined by the developers for the different types of
> registers provide a better source for an equality relation on registers
> than `type-of.' That's why I would rather use references to the
> appropriate method implementations for hashing.
You mean you want to take a hash of the actual code?
That will change when the code is tweaked, so I don't think you're going
to like that.
You're probably better off with a hash of (format "%S" (type-of FOO)).
Stefan
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Colorize objects by method dispatch type
2020-04-02 3:00 ` Stefan Monnier
@ 2020-04-02 3:45 ` Michael Heerdegen
2020-04-02 12:56 ` Stefan Monnier
0 siblings, 1 reply; 8+ messages in thread
From: Michael Heerdegen @ 2020-04-02 3:45 UTC (permalink / raw)
To: Stefan Monnier; +Cc: help-gnu-emacs
Stefan Monnier <monnier@iro.umontreal.ca> writes:
> You mean you want to take a hash of the actual code?
> That will change when the code is tweaked, so I don't think you're going
> to like that.
Not necessarily the code, better would be a reference to the code, or
the entry point of the code, if something like that is available.
Michael.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Colorize objects by method dispatch type
2020-04-02 3:45 ` Michael Heerdegen
@ 2020-04-02 12:56 ` Stefan Monnier
2020-04-02 23:46 ` Michael Heerdegen
0 siblings, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2020-04-02 12:56 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
>> You mean you want to take a hash of the actual code?
>> That will change when the code is tweaked, so I don't think you're going
>> to like that.
>
> Not necessarily the code, better would be a reference to the code, or
> the entry point of the code, if something like that is available.
I must be missing something: you do want to get some sort of a hash, right?
And you do want that hash to be stable over time, so you can get used to
which color corresponds to what, right?
Hashing a reference will not give you a stable result.
As for "entry point", I don't really what that would be but it doesn't
sound like it'd be something that results in a stable hash either.
Also it's not clear to me exactly which part of `type-of` you don't like.
Stefan
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Colorize objects by method dispatch type
2020-04-02 12:56 ` Stefan Monnier
@ 2020-04-02 23:46 ` Michael Heerdegen
2020-04-03 16:01 ` Stefan Monnier
0 siblings, 1 reply; 8+ messages in thread
From: Michael Heerdegen @ 2020-04-02 23:46 UTC (permalink / raw)
To: Stefan Monnier; +Cc: help-gnu-emacs
Stefan Monnier <monnier@iro.umontreal.ca> writes:
> Also it's not clear to me exactly which part of `type-of` you don't like.
`type-of' should be ok in most cases, but I'm looking for an alternative
for cases where e.g. `type-of' just says `cons' whereby the objects are
actually very different things that are identified with a tag as first
element or so. It's just that these are not represented with "official"
classes defined with defstruct. If the code in question doesn't use
methods but just a `cond' to distinguish I'm lost anyway but if it uses
methods than I could use the signature of the chosen method (for a
specified generic name) for hashing, or the signature of the most
specific method implementation. Then the color would only change if the
hierarchy of implementations changes, and in that case a changed color
would be acceptable.
Michael.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Colorize objects by method dispatch type
2020-04-02 23:46 ` Michael Heerdegen
@ 2020-04-03 16:01 ` Stefan Monnier
2020-04-03 23:23 ` Michael Heerdegen
0 siblings, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2020-04-03 16:01 UTC (permalink / raw)
To: help-gnu-emacs
>> Also it's not clear to me exactly which part of `type-of` you don't like.
>
> `type-of' should be ok in most cases, but I'm looking for an alternative
> for cases where e.g. `type-of' just says `cons' whereby the objects are
> actually very different things that are identified with a tag as first
> element or so. It's just that these are not represented with "official"
> classes defined with defstruct. If the code in question doesn't use
> methods but just a `cond' to distinguish I'm lost anyway but if it uses
> methods than I could use the signature of the chosen method (for a
> specified generic name) for hashing, or the signature of the most
> specific method implementation. Then the color would only change if the
> hierarchy of implementations changes, and in that case a changed color
> would be acceptable.
Hmm... so I guess ideally, you'd like to get the list of *specializers*
that match your object when you call a particular generic function.
You could write code to do that, but you'd have to dig into the innards
of cl-generic.el: while cl-generic.el does need to compute something
like that in order to perform the dispatch, the current code is not
designed to give you just that information (it's only used as part of
the code that does the method dispatch and the construction of the
combined method).
Here's how it works:
- each specializer has (set of) matching generalizer. You can get that
with `cl-generic-generalizers`.
- The generalizer has code to take an arbitrary value and extra some
"tag" from it. This `tag` is what is used in the usual dispatch (so
computation of the tag is expected to be fast and the tag is then
looked up in a hash-table). In a typical case, the tag is the
`type-of` the object.
You can get this code with `cl--generic-generalizer-tagcode-function`.
- The generalizer also has code to take such a tag and return the set of
specializers that match it.
You can get this code with `cl--generic-generalizer-specializers-function`.
So you'd need to take get all the methods of the generic function you're
interested it, then for each one extract the specializer of the argument
you're interested in (usually the first argument). That gives you
the list of specializers which corresponds to the list of possible
"classes" into which you want to divide your values.
Then you use `cl-generic-generalizers` to turn those specializers
into generalizers, then use `cl--generic-generalizer-tagcode-function`
and `cl--generic-generalizer-specializers-function` to get the code that
returns the set of specializers that match your value.
For completeness, you can then intersect that set with your original set
of specializers, in case you want to avoid the risk that
`cl--generic-generalizer-specializers-function` returns "too
fine-grained specializers". E.g. without this intersection you risk
falling into a situation where every value gets a different set of
specializers (because every value V matches its own specializer `(eql
V)`).
Then again, maybe
(defun my-type-hash (v)
(let ((type (if (and (consp v) (symbolp (car v))) (car v) (type-of v))))
(sxhash-equal (symbol-name type))))
is not such a bad idea after all ;-)
Stefan
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Colorize objects by method dispatch type
2020-04-03 16:01 ` Stefan Monnier
@ 2020-04-03 23:23 ` Michael Heerdegen
0 siblings, 0 replies; 8+ messages in thread
From: Michael Heerdegen @ 2020-04-03 23:23 UTC (permalink / raw)
To: Stefan Monnier; +Cc: help-gnu-emacs
Stefan Monnier <monnier@iro.umontreal.ca> writes:
> Then again, maybe
>
> (defun my-type-hash (v)
> (let ((type (if (and (consp v) (symbolp (car v))) (car v) (type-of v))))
> (sxhash-equal (symbol-name type))))
>
> is not such a bad idea after all ;-)
Hmm, yes - probably, I'll go with that :-)
Thanks for the elaborations.
Michael.
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2020-04-03 23:23 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-04-01 20:25 Colorize objects by method dispatch type Michael Heerdegen
2020-04-01 21:46 ` Michael Heerdegen
2020-04-02 3:00 ` Stefan Monnier
2020-04-02 3:45 ` Michael Heerdegen
2020-04-02 12:56 ` Stefan Monnier
2020-04-02 23:46 ` Michael Heerdegen
2020-04-03 16:01 ` Stefan Monnier
2020-04-03 23:23 ` Michael Heerdegen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).