flan/test/programs/generic-reject.flan

16 lines
780 B
Plaintext

;;;; The refusal, at the definition and not at a call site.
;;;;
;;;; A generic body is checked once with its type variables abstract, so an
;;;; operator the variable is not declared to support is refused here, naming
;;;; the variable — rather than at whichever call site first instantiated it
;;;; at a type that did not work. That is not Odin's model: Odin checks a
;;;; polymorphic body only per instantiation, so (+ a b) over a $T compiles
;;;; there and fails only if someone reaches it at a type without +.
;;;;
;;;; The way out is either predicate — {:where (is-numeric $t)} — or the
;;;; parameter, a (Fn [$t $t] $t) the caller supplies. Neither is written
;;;; here, which is the point.
(defn add2 [a $t b $t] $t (+ a b))
(defn main [] () (println (add2 1 2)))