From: 046-301-5902@kylheku.com   
      
   On 2026-02-06, Michael S wrote:   
   > On Fri, 6 Feb 2026 12:39:55 +0000   
   > Bart wrote:   
   >   
   >> On 06/02/2026 05:10, Keith Thompson wrote:   
   >> > Bart writes:   
   >> > [...]   
   >> >> /Some/ compilers with /some/ options will /sometimes/ tell you when   
   >> >> you've got it wrong.   
   >> >>   
   >> >> But you first have to make an educated guess, or put in some dummy   
   >> >> format code.   
   >> >>   
   >> >> Eventually, it will compile. Until someone else builds your   
   >> >> program, using a slightly different set of headers where certain   
   >> >> types are defined, and then it might either give compiler messages   
   >> >> that they have to fix, or it show wrong results.   
   >> >   
   >> > That's not how I do it, and I don't think it's how most programmers   
   >> > do it.   
   >> >   
   >> > I know the rules well enough that I can usually write a correct   
   >> > format string in the first place. If I make a mistake, gcc's   
   >> > warnings are a nice check.   
   >>   
   >> I guess you've never used printf-family functions via the FFI of   
   >> another language!   
   >>   
   >>   
   >   
   > Vararg via FFI? Is it really a good idea?   
      
   I support it in TXR Lisp. However, it's limited in that the FFI   
   definition is nailed to a particular choice of arguments.   
      
   For instance we could make a function foo which takes two arguments:   
   a str and an int, and calls the variadic printf.   
      
   Then we can call (foo "%d" 42). It will call printf("%d", 42).   
      
   We cannot pass fewer or more than two arguments to foo, and they have to   
   be compatible with str and int.   
      
   Demo:   
      
   $ txr   
   This is the TXR Lisp interactive listener of TXR 302.   
   Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet.   
   1> (with-dyn-lib nil   
    (deffi printf-int "printf" int (str : int)))   
   printf-int   
   2> (printf-int "%d\n" 42)   
   42   
   3   
      
   42 is output; 3 is the result value (3 characters output).   
      
   The : syntax in the deffi macro call indicates the variadic list.   
   It's not the case that we can make a variadic Lisp function pass its arguments   
   as an arbitrarily long variadic list with arbitrary types to the wrapped FFI   
   function. Fixed parameters must be declared after the colon.   
      
   A dynamic treatment could be arranged via a heavy weight wrapper mechanism   
   which   
   dynamically analyzes the actual arguments, builds a libffi function descriptor   
   on the fly, then uses it to make the call; it could be wortwhile for someone,   
   but I didn't implement such a thing. Metaprogramming tricks revolving around   
   dynamically evaluating deffi are also possible.   
      
   --   
   TXR Programming Language: http://nongnu.org/txr   
   Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal   
   Mastodon: @Kazinator@mstdn.ca   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|