Skip to content

Portable Multi Client Code Eclector De Dynamism

Ashok Khanna edited this page Jan 23, 2022 · 1 revision

12:51:45 lisp123w I am thinking of writing some 'portable' code (specifically code that can work with both CLIM & CAPI), so at some point I need to access implementation specific functions 12:52:23 lisp123w So my plan is to define the basic operations in terms of generic functions that take in an optional value, and then have the methods implement for each specific implementation 12:53:12 lisp123w Is there a particular way I should approach this? 12:55:07 lisp123w Right now, I can only think of having to have two functions, something like this: (defvar impl :clim) (defun insert-string-at-point (string) (%insert-string-at-point string impl)) .. (defgeneric %insert-string-at-point (string impl)) 12:55:14 beach lisp123w: Check out our libraries Eclector, Cluster, Trucler, etc. They all use a CLIENT required parameter that can handle that situation. 12:55:21 lisp123w beach: Thank you! 12:55:30 lisp123w I will do that now 12:55:42 beach This technique has turned out to be extremely flexible. 12:55:48 pjb lisp123w: or: (defmethod foo (arg &optional (impl impl)) …) 12:56:32 lisp123w pjb: I was thinking that, but if I remember, can't dispatch on optionals? 12:56:49 phoe it isn't an optional parameter 12:57:24 phoe (defgeneric foo-with-client (client ...)) (defun foo (...) (foo-with-client default-client ...)) 12:57:59 beach Yes, we do that in Eclector for instance, where we can't alter the signature of READ. 12:58:11 phoe where default-client is both something you provide and something that the clients of your library can rebind themselves as a dynavar 12:58:16 lisp123w phoe: Yeah I was thinking just that 13:00:19 beach So for Eclector, we can do (let ((eclector:client sicl)) (eclector:read...)) 13:01:24 lisp123w I see 13:02:45 beach In our case, the default methods provided by the library do not specialize to the CLIENT parameter, but the same client code that binds client can provide extending or overriding methods that specialize to the class of their particular client. 13:03:10 beach Like (defmethod eclector:interpret-token ((client sicl) ...) ...) 13:04:08 lisp123w That's an interesting idea 13:04:22 lisp123w Very interesting actually 13:05:03 beach We are very pleased with this "discovery". It allows for code for multiple clients to coexist in the same Common Lisp image. 13:05:57 Nilby and to make it look nice, you can (defmacro foo (..) (foo-with-client default-client ...)) 13:06:58 beach Well, in the case of Eclector, READ is defined to be a function, so it can't be a macro. 13:07:34 phoe Nilby: that'll badly break #'foo 13:08:43 lisp123w beach: Is there a reason for the local binding (let ((client client))...) in particular? 13:09:12 phoe lisp123w: where do you mean? 13:09:28 lisp123w I am just looking at (defun read-aux ...) in read.lisp in Eclector 13:09:48 beach Let me look at it... 13:09:59 lisp123w I would have done it as &optional (client client), but maybe there's a particular reason (from what I understand they should be the same) 13:10:26 lisp123w (well not actually the same, but the effect being the same if client is not supplied in as an optional) 13:10:31 phoe it's surely a way to de-dynamify the binding, because CLIENT is a lexical variable 13:11:14 beach lisp123w: It is a bit more expensive to access a dynamic variable than a lexical one. 13:11:16 phoe which kind of makes sense because you only need the dynamic binding on the boundary of your system, so you can effectively pass extra function arguments this way 13:11:34 lisp123w beach: Got it! Thanks (thakns phoe too) 13:11:50 phoe so what beach said + you guard your code against some funky re-binding that can cause one part of your code to be executed with one client and another part with another 13:12:22 beach Yes, you send a message to the person reading your code that you intend to use the same value of client in the entire function. 13:12:24 phoe that's what you get when the client object becomes bound to a lexical variable instead of being accessed by a dynavar 13:13:03 lisp123w phoe: Thanks, is that mostly an issue in multiple processes? 13:13:08 phoe not really 13:13:20 phoe every thread has its own set of dynamic bindings, so that isn't a problem 13:13:51 lisp123w Ah so it is just taking a 'snapshot' of its value at that point in time? 13:13:55 phoe yes 13:14:02 phoe that's what I mean by de-dynamifying 13:14:02 lisp123w Got it, thanks! 13:14:07 beach lisp123w: Imagine you had (foo client) (bar) (baz client). 13:14:23 beach You don't know whether BAR might do (setf client ...). 13:14:29 phoe you cannot make a closure over a dynamic variable because it's too dynamic to be closed over™ 13:14:37 lisp123w beach: Nice example, I never really thought of this point until now 13:15:08 phoe you need to grab the value and stuff it into a lexical variable which is less prone to dynamism such as random-seeming changes 13:15:11 phoe which is what happens here 13:15:20 beach It is not a big deal usually, and in read-aux I don't think this case can happen. 13:15:50 lisp123w I see. Still a good practice to follow where relevant 13:16:01 beach Indeed. 13:16:55 beach lisp123w: Eclector is a great library to study, for many reasons. 13:20:18 lisp123w beach: Thanks for the heads up, will do! I'm now at the stage where I'm trying to learn from other code vs. learning the concepts 13:20:42 phantomics Looking for people's opinions on an engineering issue that's come up 13:20:50 beach lisp123w: That's excellent! 13:21:05 lisp123w Which Lisp makes it possible since its so open and easy to introspect :) 13:21:15 beach Sure.

Clone this wiki locally