/trunk/Doc/Manual/Chicken.html

# · HTML · 597 lines · 471 code · 123 blank · 3 comment · 0 complexity · e86d7317c9f852946d16a3186712d8e1 MD5 · raw file

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <!-- Hand-written HTML -->
  3. <html>
  4. <head>
  5. <title>SWIG and Chicken</title>
  6. <link rel="stylesheet" type="text/css" href="style.css">
  7. </head>
  8. <body bgcolor="#ffffff">
  9. <H1><a name="Chicken"></a>20 SWIG and Chicken</H1>
  10. <!-- INDEX -->
  11. <div class="sectiontoc">
  12. <ul>
  13. <li><a href="#Chicken_nn2">Preliminaries</a>
  14. <ul>
  15. <li><a href="#Chicken_nn3">Running SWIG in C mode</a>
  16. <li><a href="#Chicken_nn4">Running SWIG in C++ mode</a>
  17. </ul>
  18. <li><a href="#Chicken_nn5">Code Generation</a>
  19. <ul>
  20. <li><a href="#Chicken_nn6">Naming Conventions</a>
  21. <li><a href="#Chicken_nn7">Modules</a>
  22. <li><a href="#Chicken_nn8">Constants and Variables</a>
  23. <li><a href="#Chicken_nn9">Functions</a>
  24. <li><a href="#Chicken_nn10">Exceptions</a>
  25. </ul>
  26. <li><a href="#Chicken_nn11">TinyCLOS</a>
  27. <li><a href="#Chicken_nn12">Linkage</a>
  28. <ul>
  29. <li><a href="#Chicken_nn13">Static binary or shared library linked at compile time</a>
  30. <li><a href="#Chicken_nn14">Building chicken extension libraries</a>
  31. <li><a href="#Chicken_nn15">Linking multiple SWIG modules with TinyCLOS</a>
  32. </ul>
  33. <li><a href="#Chicken_nn16">Typemaps</a>
  34. <li><a href="#Chicken_nn17">Pointers</a>
  35. <ul>
  36. <li><a href="#Chicken_collection">Garbage collection</a>
  37. </ul>
  38. <li><a href="#Chicken_nn18">Unsupported features and known problems</a>
  39. <ul>
  40. <li><a href="#Chicken_nn19">TinyCLOS problems with Chicken version &lt;= 1.92</a>
  41. </ul>
  42. </ul>
  43. </div>
  44. <!-- INDEX -->
  45. <p>
  46. This chapter describes SWIG's support of CHICKEN. CHICKEN is a
  47. Scheme-to-C compiler supporting most of the language features as
  48. defined in the <i>Revised^5 Report on Scheme</i>. Its main
  49. attributes are that it
  50. </p>
  51. <ol>
  52. <li>generates portable C code</li>
  53. <li>includes a customizable interpreter</li>
  54. <li>links to C libraries with a simple Foreign Function Interface</li>
  55. <li>supports full tail-recursion and first-class continuations</li>
  56. </ol>
  57. <p>
  58. When confronted with a large C library, CHICKEN users can use
  59. SWIG to generate CHICKEN wrappers for the C library. However,
  60. the real advantages of using SWIG with CHICKEN are its
  61. <strong>support for C++</strong> -- object-oriented code is
  62. difficult to wrap by hand in CHICKEN -- and its <strong>typed
  63. pointer representation</strong>, essential for C and C++
  64. libraries involving structures or classes.
  65. </p>
  66. <H2><a name="Chicken_nn2"></a>20.1 Preliminaries</H2>
  67. <p>
  68. CHICKEN support was introduced to SWIG in version 1.3.18. SWIG
  69. relies on some recent additions to CHICKEN, which are only
  70. present in releases of CHICKEN with version number
  71. <strong>greater than or equal to 1.89</strong>.
  72. To use a chicken version between 1.40 and 1.89, see the <a href="#Chicken_collection">Garbage collection</a>
  73. section below.
  74. </p>
  75. <p>
  76. You may want to look at any of the examples in Examples/chicken/
  77. directory for the basic steps to run SWIG CHICKEN.
  78. </p>
  79. <H3><a name="Chicken_nn3"></a>20.1.1 Running SWIG in C mode</H3>
  80. <p>
  81. To run SWIG CHICKEN in C mode, use
  82. the -chicken option.
  83. </p>
  84. <div class="shell">
  85. <pre>% swig -chicken example.i</pre>
  86. </div>
  87. <p>
  88. To allow the wrapper to take advantage of future CHICKEN code
  89. generation improvements, part of the wrapper is direct CHICKEN
  90. function calls (<tt>example_wrap.c</tt>) and part is CHICKEN
  91. Scheme (<tt>example.scm</tt>). The basic Scheme code must
  92. be compiled to C using your system's CHICKEN compiler or
  93. both files can be compiled directly using the much simpler <tt>csc</tt>.
  94. </p>
  95. <div class="shell">
  96. <pre>
  97. % chicken example.scm -output-file oexample.c
  98. </pre>
  99. </div>
  100. <p>
  101. So for the C mode of SWIG CHICKEN, <tt>example_wrap.c</tt> and
  102. <tt>oexample.c</tt> are the files that must be compiled to
  103. object files and linked into your project.
  104. </p>
  105. <H3><a name="Chicken_nn4"></a>20.1.2 Running SWIG in C++ mode</H3>
  106. <p>
  107. To run SWIG CHICKEN in C++ mode, use
  108. the -chicken -c++ option.
  109. </p>
  110. <div class="shell">
  111. <pre>% swig -chicken -c++ example.i</pre>
  112. </div>
  113. <p>
  114. This will generate <tt>example_wrap.cxx</tt> and
  115. <tt>example.scm</tt>. The basic Scheme code must be
  116. compiled to C using your system's CHICKEN compiler or
  117. both files can be compiled directly using the much simpler <tt>csc</tt>.
  118. </p>
  119. <div class="shell">
  120. <pre>% chicken example.scm -output-file oexample.c</pre>
  121. </div>
  122. <p>
  123. So for the C++ mode of SWIG CHICKEN, <tt>example_wrap.cxx</tt>
  124. and <tt>oexample.c</tt> are the files that must be compiled to
  125. object files and linked into your project.
  126. </p>
  127. <H2><a name="Chicken_nn5"></a>20.2 Code Generation</H2>
  128. <H3><a name="Chicken_nn6"></a>20.2.1 Naming Conventions</H3>
  129. <p>
  130. Given a C variable, function or constant declaration named
  131. <tt>Foo_Bar</tt>, the declaration will be available
  132. in CHICKEN as an identifier ending with
  133. <tt>Foo-Bar</tt>. That is, an underscore is converted
  134. to a dash.
  135. </p>
  136. <p>
  137. You may control what the CHICKEN identifier will be by using the
  138. <tt>%rename</tt> SWIG directive in the SWIG interface file.
  139. </p>
  140. <H3><a name="Chicken_nn7"></a>20.2.2 Modules</H3>
  141. <p>
  142. The name of the module must be declared one of two ways:
  143. <ul>
  144. <li>Placing <tt>%module example</tt> in the SWIG interface
  145. file.</li>
  146. <li>Using <tt>-module example</tt> on the SWIG command
  147. line.</li>
  148. </ul>
  149. <p>
  150. The generated example.scm file then exports <code>(declare (unit modulename))</code>.
  151. If you do not want SWIG to export the <code>(declare (unit modulename))</code>, pass
  152. the -nounit option to SWIG.
  153. <p>
  154. CHICKEN will be able to access the module using the <code>(declare
  155. (uses <i>modulename</i>))</code> CHICKEN Scheme form.
  156. </p>
  157. <H3><a name="Chicken_nn8"></a>20.2.3 Constants and Variables</H3>
  158. <p>
  159. Constants may be created using any of the four constructs in
  160. the interface file:
  161. </p>
  162. <ol>
  163. <li><code>#define MYCONSTANT1 ...</code></li>
  164. <li><code>%constant int MYCONSTANT2 = ...</code></li>
  165. <li><code>const int MYCONSTANT3 = ...</code></li>
  166. <li><code>enum { MYCONSTANT4 = ... };</code></li>
  167. </ol>
  168. <p>
  169. In all cases, the constants may be accessed from within CHICKEN
  170. using the form <tt>(MYCONSTANT1)</tt>; that is, the constants
  171. may be accessed using the read-only parameter form.
  172. </p>
  173. <p>
  174. Variables are accessed using the full parameter form.
  175. For example, to set the C variable "int my_variable;", use the
  176. Scheme form <tt>(my-variable 2345)</tt>. To get the C variable,
  177. use <tt>(my-variable)</tt>.
  178. </p>
  179. <p>
  180. The <tt>%feature("constasvar")</tt> can be applied to any constant
  181. or immutable variable. Instead of exporting the constant as
  182. a function that must be called, the constant will appear as a
  183. scheme variable. This causes the generated .scm file to just contain the code
  184. <tt>(set! MYCONSTANT1 (MYCONSTANT1))</tt>. See
  185. <a href="Customization.html#Customization_features">Features and the %feature directive</a>
  186. for info on how to apply the %feature.
  187. </p>
  188. <H3><a name="Chicken_nn9"></a>20.2.4 Functions</H3>
  189. <p>
  190. C functions declared in the SWIG interface file will have
  191. corresponding CHICKEN Scheme procedures. For example, the C
  192. function "int sqrt(double x);" will be available using the
  193. Scheme form <tt>(sqrt 2345.0)</tt>. A <code>void</code> return
  194. value will give C_SCHEME_UNDEFINED as a result.
  195. </p>
  196. <p>
  197. A function may return more than one value by using the
  198. <code>OUTPUT</code> specifier (see Lib/chicken/typemaps.i).
  199. They will be returned as multiple values using <code>(values)</code> if there is more than one
  200. result (that is, a non-void return value and at least one argout
  201. parameter, or a void return value and at least two argout
  202. parameters). The return values can then be accessed with <code>(call-with-values)</code>.
  203. </p>
  204. <H3><a name="Chicken_nn10"></a>20.2.5 Exceptions</H3>
  205. <p>The SWIG chicken module has support for exceptions thrown from
  206. C or C++ code to be caught in scheme.
  207. See <a href="Customization.html#Customization_exception">Exception handling with %exception</a>
  208. for more information about declaring exceptions in the interface file.
  209. </p>
  210. <p>Chicken supports both the <code>SWIG_exception(int code, const char *msg)</code> interface
  211. as well as a <code>SWIG_ThrowException(C_word val)</code> function for throwing exceptions from
  212. inside the %exception blocks. <code>SWIG_exception</code> will throw a list consisting of the code
  213. (as an integer) and the message. Both of these will throw an exception using <code>(abort)</code>,
  214. which can be handled by <code>(handle-exceptions)</code>. See
  215. the Chicken manual on Exceptions
  216. and <a href="http://srfi.schemers.org/srfi-12/srfi-12.html">SFRI-12</a>. Since the exception values are thrown
  217. directly, if <code>(condition-case)</code> is used to catch an exception the exception will come through in the <code>val ()</code> case.
  218. </p>
  219. <p>The following simple module</p>
  220. <div class="code"><pre>
  221. %module exception_test
  222. %inline %{
  223. void test_throw(int i) throws (int) {
  224. if (i == 1) throw 15;
  225. }
  226. %}
  227. </pre></div>
  228. <p>could be run with</p>
  229. <div class="targetlang"><pre>
  230. (handle-exceptions exvar
  231. (if (= exvar 15)
  232. (print "Correct!")
  233. (print "Threw something else " exvar))
  234. (test-throw 1))
  235. </pre></div>
  236. <H2><a name="Chicken_nn11"></a>20.3 TinyCLOS</H2>
  237. <p>
  238. The author of TinyCLOS, Gregor Kiczales, describes TinyCLOS as:
  239. "Tiny CLOS is a Scheme implementation of a `kernelized' CLOS, with a
  240. metaobject protocol. The implementation is even simpler than
  241. the simple CLOS found in `The Art of the Metaobject Protocol,'
  242. weighing in at around 850 lines of code, including (some)
  243. comments and documentation."
  244. </p>
  245. <p>
  246. Almost all good Scheme books describe how to use metaobjects and
  247. generic procedures to implement an object-oriented Scheme
  248. system. Please consult a Scheme book if you are unfamiliar
  249. with the concept.
  250. </p>
  251. <p>
  252. CHICKEN has a modified version of TinyCLOS, which SWIG CHICKEN
  253. uses if the -proxy argument is given. If -proxy is passed, then
  254. the generated example.scm file will contain TinyCLOS class definitions.
  255. A class named Foo is declared as &lt;Foo&gt;, and each member variable
  256. is allocated a slot. Member functions are exported as generic functions.
  257. <p>
  258. Primitive symbols and functions (the interface that would be presented if
  259. -proxy was not passed) are hidden and no longer accessible. If the -unhideprimitive
  260. command line argument is passed to SWIG, then the primitive symbols will be
  261. available, but each will be prefixed by the string "primitive:"
  262. <p>
  263. The exported symbol names can be controlled with the -closprefix and -useclassprefix arguments.
  264. If -useclassprefix is passed to SWIG, every member function will be generated with the class name
  265. as a prefix. If the -closprefix mymod: argument is passed to SWIG, then the exported functions will
  266. be prefixed by the string "mymod:". If -useclassprefix is passed, -closprefix is ignored.
  267. </p>
  268. <H2><a name="Chicken_nn12"></a>20.4 Linkage</H2>
  269. <p>
  270. Please refer to <em>CHICKEN - A practical and portable Scheme
  271. system - User's manual</em> for detailed help on how to link
  272. object files to create a CHICKEN Scheme program. Briefly, to
  273. link object files, be sure to add <tt>`chicken-config
  274. -extra-libs -libs`</tt> or <tt>`chicken-config -shared
  275. -extra-libs -libs`</tt>to your linker options. Use the
  276. <tt>-shared</tt> option if you want to create a dynamically
  277. loadable module. You might also want to use the much simpler
  278. <tt>csc</tt> or <tt>csc.bat</tt>.
  279. </p>
  280. <p>Each scheme file that is generated
  281. by SWIG contains <code>(declare (uses <i>modname</i>))</code>. This means that to load the
  282. module from scheme code, the code must include <code>(declare (uses <i>modname</i>))</code>.
  283. </p>
  284. <H3><a name="Chicken_nn13"></a>20.4.1 Static binary or shared library linked at compile time</H3>
  285. <p>We can easily use csc to build a static binary.</p>
  286. <div class="shell">
  287. <pre>
  288. $ swig -chicken example.i
  289. $ csc -v example.scm example_impl.c example_wrap.c test_script.scm -o example
  290. $ ./example
  291. </pre>
  292. </div>
  293. <p>Similar to the above, any number of <tt>module.scm</tt> files could be compiled
  294. into a shared library, and then that shared library linked when compiling the
  295. main application.</p>
  296. <div class="shell">
  297. <pre>
  298. $ swig -chicken example.i
  299. $ csc -sv example.scm example_wrap.c example_impl.c -o example.so
  300. </pre>
  301. </div>
  302. <p>The <tt>example.so</tt> file can then linked with <tt>test_script.scm</tt> when it
  303. is compiled, in which case <tt>test_script.scm</tt> must have <code>(declare (uses example))</code>.
  304. Multiple SWIG modules could have been linked into <tt>example.so</tt> and each
  305. one accessed with a <code>(declare (uses ... ))</code>.
  306. </p>
  307. <div class="shell">
  308. <pre>
  309. $ csc -v test_script.scm -lexample
  310. </pre>
  311. </div>
  312. <p>An alternative is that the test_script.scm can have the code <code>(load-library 'example "example.so")</code>,
  313. in which case the test script does not need to be linked with example.so. The test_script.scm file can then
  314. be run with <tt>csi</tt>.
  315. </p>
  316. <H3><a name="Chicken_nn14"></a>20.4.2 Building chicken extension libraries</H3>
  317. <p>Building a shared library like in the above section only works if the library
  318. is linked at compile time with a script containing <code>(declare (uses ...))</code> or is
  319. loaded explicitly with <code>(load-library 'example "example.so")</code>. It is
  320. not the format that CHICKEN expects for extension libraries and eggs. The problem is the
  321. <code>(declare (unit <i>modname</i>))</code> inside the <tt>modname.scm</tt> file. There are
  322. two possible solutions to this.</p>
  323. <p>First, SWIG accepts a <tt>-nounit</tt> argument, in which case the <code>(declare (unit <i>modname</i>))</code>
  324. is not generated. Then, the <tt>modname.scm</tt> and <tt>modname_wrap.c</tt> files <b>must</b> be compiled into
  325. their own shared library.</p>
  326. <div class="shell">
  327. <pre>
  328. $ csc -sv modname.scm modname_wrap.c modname_impl.c -o modname.so
  329. </pre>
  330. </div>
  331. <p>This library can then be loaded by scheme code with the <code>(require 'modname)</code> function.
  332. See the
  333. Loading-extension-libraries in the eval unit inside the CHICKEN manual for more information.</p>
  334. <p>Another alternative is to run SWIG normally and create a scheme file that contains <code>(declare (uses <i>modname</i>))</code>
  335. and then compile that file into the shared library as well. For example, inside the <tt>mod_load.scm</tt> file,</p>
  336. <div class="targetlang">
  337. <pre>
  338. (declare (uses mod1))
  339. (declare (uses mod2))
  340. </pre>
  341. </div>
  342. <p>Which would then be compiled with</p>
  343. <div class="shell">
  344. <pre>
  345. $ swig -chicken mod1.i
  346. $ swig -chicken mod2.i
  347. $ csc -sv mod_load.scm mod1.scm mod2.scm mod1_wrap.c mod2_wrap.c mod1_impl.c mod2_impl.c -o mod.so
  348. </pre>
  349. </div>
  350. <p>Then the extension library can be loaded with <code>(require 'mod)</code>. As we can see here,
  351. <tt>mod_load.scm</tt> contains the code that gets executed when the module is loaded. All this code
  352. does is load both mod1 and mod2. As we can see, this technique is more useful when you want to
  353. combine a few SWIG modules into one chicken extension library, especially if modules are related by
  354. <code>%import</code></p>
  355. <p>In either method, the files that are compiled into the shared library could also be
  356. packaged into an egg. The <tt>mod1_wrap.c</tt> and <tt>mod2_wrap.c</tt> files that are created by SWIG
  357. are stand alone and do not need SWIG to be installed to be compiled. Thus the egg could be
  358. distributed and used by anyone, even if SWIG is not installed.</p>
  359. <p>See the <tt>Examples/chicken/egg</tt> directory in the SWIG source for an example that builds
  360. two eggs, one using the first method and one using the second method.</p>
  361. <H3><a name="Chicken_nn15"></a>20.4.3 Linking multiple SWIG modules with TinyCLOS</H3>
  362. <p>Linking together multiple modules that share type information using the <code>%import</code>
  363. directive while also using <tt>-proxy</tt> is more complicated. For example, if <tt>mod2.i</tt> imports <tt>mod1.i</tt>, then the
  364. <tt>mod2.scm</tt> file contains references to symbols declared in <tt>mod1.scm</tt>,
  365. and thus a <code>(declare (uses <i>mod1</i>))</code> or <code>(require '<i>mod1</i>)</code> must be exported
  366. to the top of <tt>mod2.scm</tt>. By default, when SWIG encounters an <code>%import "modname.i"</code> directive,
  367. it exports <code>(declare (uses <i>modname</i>))</code> into the scm file. This works fine unless mod1 was compiled with
  368. the <tt>-nounit</tt> argument or was compiled into an extension library with other modules under a different name.</p>
  369. <p>One option is to override the automatic generation of <code>(declare (uses mod1))</code>
  370. by passing the <tt>-noclosuses</tt> option to SWIG when compiling <tt>mod2.i</tt>.
  371. SWIG then provides the <code>%insert(closprefix) %{ %}</code> directive. Any scheme code inside that directive is inserted into the
  372. generated .scm file, and if <tt>mod1</tt> was compiled with <tt>-nounit</tt>, the directive should contain <code>(require 'mod1)</code>.
  373. This option allows for mixed loading as well, where some modules are imported with <code>(declare (uses <i>modname</i>))</code>
  374. (which means they were compiled without -nounit) and some are imported with <code>(require 'modname)</code>.</p>
  375. <p>The other option is to use the second idea in the above section. Compile all the modules normally, without any
  376. <code>%insert(closprefix)</code>, <tt>-nounit</tt>, or <tt>-noclosuses</tt>. Then the modules will import each other correctly
  377. with <code>(declare (uses ...))</code>.
  378. To create an extension library or an egg, just create a <tt>module_load.scm</tt> file that <code>(declare (uses ...))</code>
  379. all the modules.</p>
  380. <H2><a name="Chicken_nn16"></a>20.5 Typemaps</H2>
  381. <p>
  382. The Chicken module handles all types via typemaps. This information is
  383. read from <code>Lib/chicken/typemaps.i</code> and
  384. <code>Lib/chicken/chicken.swg</code>.
  385. </p>
  386. <H2><a name="Chicken_nn17"></a>20.6 Pointers</H2>
  387. <p>
  388. For pointer types, SWIG uses CHICKEN tagged pointers.
  389. A tagged pointer is an ordinary CHICKEN pointer with an
  390. extra slot for a void *. With SWIG
  391. CHICKEN, this void * is a pointer to a type-info
  392. structure. So each pointer used as input or output from
  393. the SWIG-generated CHICKEN wrappers will have type
  394. information attached to it. This will let the wrappers
  395. correctly determine which method should be called
  396. according to the object type hierarchy exposed in the SWIG
  397. interface files.
  398. </p>
  399. <p>
  400. To construct a Scheme object from a C pointer, the wrapper code
  401. calls the function
  402. <code>SWIG_NewPointerObj(void *ptr, swig_type_info *type, int owner)</code>,
  403. The function that calls <code>SWIG_NewPointerObj</code> must have a variable declared
  404. <code>C_word *known_space = C_alloc(C_SIZEOF_SWIG_POINTER);</code>
  405. It is ok to call <code>SWIG_NewPointerObj</code> more than once,
  406. just make sure known_space has enough space for all the created pointers.
  407. </p>
  408. <p>
  409. To get the pointer represented by a CHICKEN tagged pointer, the
  410. wrapper code calls the function
  411. <code>SWIG_ConvertPtr(C_word s, void **result, swig_type_info *type, int flags)</code>,
  412. passing a pointer to a struct representing the expected pointer
  413. type. flags is either zero or SWIG_POINTER_DISOWN (see below).
  414. </p>
  415. <H3><a name="Chicken_collection"></a>20.6.1 Garbage collection</H3>
  416. <p>If the owner flag passed to <code>SWIG_NewPointerObj</code> is 1, <code>NewPointerObj</code> will add a
  417. finalizer to the type which will call the destructor or delete method of
  418. that type. The destructor and delete functions are no longer exported for
  419. use in scheme code, instead SWIG and chicken manage pointers.
  420. In situations where SWIG knows that a function is returning a type that should
  421. be garbage collected, SWIG will automatically set the owner flag to 1. For other functions,
  422. the <code>%newobject</code> directive must be specified for functions whose return values
  423. should be garbage collected. See
  424. <a href="Customization.html#Customization_ownership">Object ownership and %newobject</a> for more information.
  425. </p>
  426. <p>In situations where a C or C++ function will assume ownership of a pointer, and thus
  427. chicken should no longer garbage collect it, SWIG provides the <code>DISOWN</code> input typemap.
  428. After applying this typemap (see the <a href="Typemaps.html#Typemaps">Typemaps chapter</a> for more information on how to apply typemaps),
  429. any pointer that gets passed in will no longer be garbage collected.
  430. An object is disowned by passing the <code>SWIG_POINTER_DISOWN</code> flag to <code>SWIG_ConvertPtr</code>.
  431. <b>Warning:</b> Since the lifetime of the object is now controlled by the underlying code, the object might
  432. get deleted while the scheme code still holds a pointer to it. Further use of this pointer
  433. can lead to a crash.
  434. </p>
  435. <p>Adding a finalizer function from C code was added to chicken in the 1.89 release, so garbage collection
  436. does not work for chicken versions below 1.89. If you would like the SWIG generated code to work with
  437. chicken 1.40 to 1.89, pass the <code>-nocollection</code> argument to SWIG. This will not export code
  438. inside the _wrap.c file to register finalizers, and will then export destructor functions which
  439. must be called manually.
  440. </p>
  441. <H2><a name="Chicken_nn18"></a>20.7 Unsupported features and known problems</H2>
  442. <ul>
  443. <li>No director support.</li>
  444. <li>No support for c++ standard types like std::vector.</li>
  445. <li>The TinyCLOS wrappers for overloaded functions will not work correctly when using
  446. <a href="SWIGPlus.html#SWIGPlus_default_args">%feature(compactdefaultargs)</a>.</li>
  447. </ul>
  448. <H3><a name="Chicken_nn19"></a>20.7.1 TinyCLOS problems with Chicken version &lt;= 1.92</H3>
  449. <p>In Chicken versions equal to or below 1.92, TinyCLOS has a limitation such that generic methods do not properly work on methods
  450. with different number of specializers: TinyCLOS assumes that every method added to a generic function
  451. will have the same number of specializers. SWIG generates functions with different lengths of specializers
  452. when C/C++ functions are overloaded. For example, the code</p>
  453. <div class="code">
  454. <pre>
  455. class Foo {};
  456. int foo(int a, Foo *b);
  457. int foo(int a);
  458. </pre></div>
  459. <p>will produce scheme code</p>
  460. <div class="targetlang">
  461. <pre>
  462. (define-method (foo (arg0 &lt;top&gt;) (arg1 &lt;Foo&gt;)) (<i>call primitive function</i>))
  463. (define-method (foo (arg0 &lt;top&gt;)) (<i>call primitive function</i>))
  464. </pre></div>
  465. <p>Using unpatched TinyCLOS, the second <code>(define-method)</code> will replace the first one,
  466. so calling <code>(foo 3 f)</code> will produce an error.</p>
  467. <p>There are three solutions to this. The easist is to upgrade to the latest Chicken version. Otherwise, the
  468. file <tt>Lib/chicken/tinyclos-multi-generic.patch</tt> in the SWIG source contains a patch against
  469. tinyclos.scm inside the 1.92 chicken source to add support into TinyCLOS for multi-argument generics. (This patch was accepted into Chicken)
  470. This requires chicken to be rebuilt and custom install of chicken. An alternative is the <tt>Lib/chicken/multi-generic.scm</tt>
  471. file in the SWIG source. This file can be loaded after TinyCLOS is loaded, and it will override some functions
  472. inside TinyCLOS to correctly support multi-argument generics. Please see the comments at the top of both files for more information.</p>
  473. </body>
  474. </html>