flan/test/programs/dyn-type-of.flan

37 lines
1.2 KiB
Plaintext

;;;; type-of: every value's kind as a keyword, and a class instance's class
;;;; name, which is what class-of answers for it.
(defclass point [x y])
;; A defmulti over type-of is dispatch on the kind of the first argument.
(defmulti describe [v] dyn (type-of v))
(defmethod describe :int [v] "a whole number")
(defmethod describe :text [v] "some text")
(defmethod describe :point [v] "a point")
(defmethod describe :else [v] "something else")
(defn main [] i32
(let [n (the i32 7)
f (the f32 1.5)
b (the bool true)
s (the string "typed")]
(println (type-of nil))
(println (type-of true))
(println (type-of 42))
(println (type-of 2.5))
(println (type-of "hello"))
(println (type-of [1 2 3]))
(println (type-of {:a 1}))
(println (type-of :k))
(println (type-of (point 1 2)))
;; Typed values cross into dyn first and answer the kind they become.
(println (type-of n))
(println (type-of f))
(println (type-of b))
(println (type-of s))
(println (describe 3))
(println (describe "x"))
(println (describe (point 3 4)))
(println (describe [1]))
(if (= (type-of (point 0 0)) (class-of (point 0 0))) 0 1)))