Hi,

Am 17.05.2009 um 03:43 schrieb bradford cross:

First item of business - there are no operators, operators are functions.

I think the Clojure way to do this is via multimethods: 
http://clojure.org/multimethods

I might just be naive, but it seems like more of core would need to be implemented as multimethods in order to do this.

Has this come up before?

There is Konrad's clojure.contrib.generic.arithmetic which
implements the usual arithmetic operators as multimethods.

The use might look like this:
(ns my.name.space
  (:refer-clojure :exclude (+))
  (:use [clojure.contrib.generic.arithmetic :only (+)]))

(defmethod + [String String]
  [a b]
  (str a b))

(+ "foo" "bar") ; should give "foobar" now

; For arbitrary structs, the use probably looks like this:

(defstruct my-struct :x :y)

(let [type-info {:type ::MyStruct}]
  (defn make-my-struct
    [x y]
    (with-meta (struct my-struct x y) type-info)))

(defmethod + [::MyStruct ::MyStruct]
  [a b]
  (make-my-struct (+ (:x a) (:x b)) (+ (:y a) (:y b))))

This is not tested, though, but should come close.

Sincerely
Meikel

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to