guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: overloading an existing operator in Guile


From: Maxime Devos
Subject: Re: overloading an existing operator in Guile
Date: Tue, 3 Oct 2023 20:34:49 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0



Op 03-10-2023 om 11:13 schreef Damien Mattei:
hello,
is it possible to overload an existing operator in Guile?

example overload + to concatenate vectors.

for example in Scheme (+ i admit) i can do :
; first stage overloading
(define-overload-existing-operator +)

; second stage overloading
(overload-existing-operator + vector-append (vector? vector?))

and use it like that:
(+ #(1 2 3) #(4 5 6))
'#(1 2 3 4 5 6)
(+ #(1 2 3) #(4 5) #(6 7 8 9))
'#(1 2 3 4 5 6 7 8 9)
{#(1 2) + #(3) + #(4 5 6)}
'#(1 2 3 4 5 6)

is it possible and how to do it using GOOPS (guile object oriented
programming system) , i already did some sort of thing with new object
, but not with an existing operator like + that apply to numbers only.

It's explained in ‘(guile)Methods and Generic Functions’ how to do this.

However, I very much recommend not doing this in your situation.

While this could be a valid interpretation of vector ‘addition’, here is another valid interpretation incompatible with yours:

;; element-wise addition
(+ #(1 2 3) #(4 5 6))
#(5 7 9)

Sure, you could choose vector appending in Scheme+ and document that appropriately, but so could a hypothetical SchemePlus choose element-wise addition, and then if someone imports a library using Scheme+ and also a library SchemePlus, there is ambiguity and GOOPS will get things wrong!

Instead, I propose using the symbol '++' and defining a new method named '++'. It would unambiguously mean ‘appending’ (*) instead of ‘addition’, and as an additional benefit, some other languages (^) use '++' to mean appending as well.

(*) string-append, append, vector-append, ...
(^) for example, Coq and Haskell

Best regards,
Maxime Devos.

Attachment: OpenPGP_0x49E3EE22191725EE.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]