Skip to content

Instantly share code, notes, and snippets.

@guicho271828
Last active August 29, 2015 14:15
Show Gist options
  • Save guicho271828/f034a99d494041b85afe to your computer and use it in GitHub Desktop.
Save guicho271828/f034a99d494041b85afe to your computer and use it in GitHub Desktop.
introducing typevar into common lisp
(with-typevar <coordinate> (<t>) (<vector-+>)
(defstruct <coordinate>
(x 0 :type <t>)
(y 0 :type <t>))
(declaim (ftype (function (<coordinate> <coordinate>) <coordinate>) <vector-+>))
(defun <vector-+> (v w)
(match* (v w)
(((<coordinate> x1 y1) (<coordinate> x2 y2))
(<coordinate> (+ x1 x2) (+ y1 y2))))))
(instantiate '<coordinate> fixnum)
(instantiate '<coordinate> float)
;;; expected-result:
(defstruct coordinate/fixnum
(x 0 :type fixnum)
(y 0 :type fixnum))
(declaim (ftype (function (coordinate/fixnum coordinate/fixnum) coordinate/fixnum) vector-+/fixnum))
(defun vector-+/fixnum (v w)
(match* (v w)
(((coordinate/fixnum x1 y1) (coordinate/fixnum x2 y2))
(coordinate/fixnum (+ x1 x2) (+ y1 y2))))))
(defstruct coordinate/float
(x 0 :type float)
(y 0 :type fioat))
(declaim (ftype (function (coordinate/float coordinate/float) coordinate/float) vector-+/float))
(defun vector-+/float (v w)
(match* (v w)
(((coordinate/float x1 y1) (coordinate/float x2 y2))
(coordinate/float (+ x1 x2) (+ y1 y2))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment