Jan Wedekind <[email protected]> writes:

> Hi,
> I am trying to implement n-ary operations for different types of objects.
> When specialising "+" for two arguments, it will automatically work on
> more arguments.
>
>     (use-modules (oop goops) (srfi srfi-1))
>     (define-generic +)
>     (define-method (+ (a <string>) (b <string>)) (string-append a b))
>     (+ "a" "b" "c")
>     ; "abc"
>
> However I have trouble figuring out a method definition for test which
> would result in the same behaviour.
>
>     (define (test . args) ...)
>     (define-generic test)
>     (define-method (test (a <string>) (b <string>)) (string-append a b))
>     (test "a" "b" "c")
>     ; "abc"
>
> Please let me know if there is a solution.

(use-modules (oop goops))

(define-generic test)
(define-method (test a b c . rest)
  (if (null? rest) (test (test a b) c)
      (apply test (test (test a b) c) rest)))
(define-method (test (a <string>) (b <string>)) (string-append a b))

(display (test "a" "b" "c" "d" "e"))

-- 
David Kastrup

Reply via email to