“Late binding”... how elegant, how mysterious. When you call it “run–time dispatching”, which is the same at some level of abstract reasoning, it does not sound as elegant; it sounds vulgar, it tastes of look–up tables. I have implemented late binding for record–type methods in Vicare; the code is in the current head of the master branch.
Here is how it works. First, let’s create a file containing this library:
#!vicare
(library (subdemo)
(options tagged-language)
(export greetings)
(import (vicare))
(define (greetings lang)
(display (.greetings lang))
(newline)))
and compile it:
$ vicare --compile ./subdemo.sls --output ./subdemo.fasl
the form:
(.greetings lang)
is transformed by the Scheme reader into:
(method-call greetings lang)
the expander does not know the type of the lang argument to function; so the
syntax method-call will expand to an application of the function
method-call-late-binding:
(method-call-late-binding 'greetings lang)
Now let’s create a file containing this program, which imports the (subdemo)
library and calls the greetings function:
#!vicare
(program (demo)
(options tagged-language)
(import (vicare)
(subdemo))
(define-record-type <lang>
(fields greetings))
(define {it <lang>}
(new <lang> "ciao"))
(define {en <lang>}
(new <lang> "hello"))
(define {jp <lang>}
(new <lang> "ohayo"))
(greetings it)
(greetings en)
(greetings jp)
(flush-output-port (current-output-port))
#| end of program |# )
and compile it:
$ vicare -L . --compile ./demo.sps --output ./demo
this program defines the record–type <lang> that was non–existent when the
library was compiled. The expansion of the define-record-type syntax use
automatically creates methods to access and mutate record–type fields; in this case:
an accessor method for the field ‘greetings’ is defined. Let’s run the program:
$ vicare -L . --binary-program ./demo ciao hello ohayo