Skip to content

Instantly share code, notes, and snippets.

@remexre
Created July 26, 2017 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remexre/6cacdafdec26db977c8058601a4c4883 to your computer and use it in GitHub Desktop.
Save remexre/6cacdafdec26db977c8058601a4c4883 to your computer and use it in GitHub Desktop.
oftlisp typeclasses
(deftypeclass (indexed 'a)
(fn ((get 'a) (this @) (index fixnum)))
(fn ((len fixnum) (this @))))
; An example data structure.
(deftype (append-list 'a)
(list (list 'a)))
(instance (append-list 'a) (indexed 'a)
(defn (get this index)
...)
(defn (len this)
(-> this
(map len)
sum)))
(defn (main)
; I can't find a way to get rid of the need for a type annotation to
; transform a built-in data structure into a deftype'd one. The problem is:
(def x '((1 2 3) (4 5 6) (7 8 9)))
(def y (as (append-list fixnum) x))
(assert-eq (len x) 3)
(assert-eq (len y) 9))
; And as an example of a non-required function on a typeclass:
(defn ((last 'a) (x (indexed 'a)))
(get x (1- (len x))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment