Re: Conditional dependencies
Hi,
I’m working on a library with a couple of different backends, each with a different set of required libraries. Since not all backends are relevant for all situations, it would be nice to offer users of the lib the possibility to choose which backends to use and avoid loading the dependencies that belong to the unused backends. Is there a good/clean/simple way to achieve this?
Read-time conditionals would be one way to go of course, but I wonder if there are other ways.
One good current solution is to use asdf, and to define different systems for the different configurations of backends.
You can define 3 different systems, all depending on a core system, and each on a different backend system. The core system can use any backend thru a common API. (eg. all backend systems can define a package with the same name as API).
+-------------+ +-------------+
| backend-1 |<-------| system-1 |-----+
+-------------+ +-------------+ |
|
+-------------+ +-------------+ +--->+-------------+
| backend-2 |<-------| system-2 |--------->| core |
+-------------+ +-------------+ +--->+-------------+
|
+-------------+ +-------------+ |
| backend-3 |<-------| system-3 |-----+
+-------------+ +-------------+
An alternative, is to merge backend-n with system-n, and just load the system of the backend you want. See for example, portable hemlock:
cl-user> (quick-apropos "hemlock")
#<system hemlock.base / hemlock-20161208-git / quicklisp 2018-10-18>
#<system hemlock.clx / hemlock-20161208-git / quicklisp 2018-10-18>
#<system hemlock.qt / hemlock-20161208-git / quicklisp 2018-10-18>
#<system hemlock.tty / hemlock-20161208-git / quicklisp 2018-10-18>
You would load one of hemlock.clx, hemlock.qt or hemlock.tty depending on the UI backend you want to use. Loading hemlock.base (the core) wouldn’t let you do much, since it doesn’t contain an UI.
The fundamental idea is, instead of using read-time conditionals in the sources, use the configuration management system (ie asdf) to select what source to load or not.
--
__Pascal J. Bourguignon__