unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: David Kastrup <dak@gnu.org>
To: guile-user@gnu.org
Subject: Re: GOOPS with variable number of arguments
Date: Sat, 28 Nov 2015 00:30:12 +0100	[thread overview]
Message-ID: <87r3jbxbp7.fsf@fencepost.gnu.org> (raw)
In-Reply-To: alpine.DEB.2.11.1511272226010.5617@wedemob

[-- Attachment #1: Type: text/plain, Size: 732 bytes --]

Jan Wedekind <jan@wedesoft.de> 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.


[-- Attachment #2: bab.scm --]
[-- Type: text/plain, Size: 276 bytes --]

(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"))

[-- Attachment #3: Type: text/plain, Size: 20 bytes --]



-- 
David Kastrup

  reply	other threads:[~2015-11-27 23:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-27 22:58 GOOPS with variable number of arguments Jan Wedekind
2015-11-27 23:30 ` David Kastrup [this message]
2015-11-28 22:24   ` Jan Wedekind

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r3jbxbp7.fsf@fencepost.gnu.org \
    --to=dak@gnu.org \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).