convex
convex copied to clipboard
Allow keywords as functions on any type
I realized this was not allowed in Convex Lisp, yet it is often convenient:
(:foo 42)
;; Cast error: Can't convert argument at position 1 to type DataStructure.
In Clojure, this returns nil while get throws similarly. It is convenient in an untyped language because it allows to tag any value, essentially without having to check defensively if something is a map. If ensuring that something is a map is needed, then get can be used.
I think if this happens, it is probably a bug? Easy to miss that you are applying a keyword to the wrong type of value, so I am not sure we want to return nil and make it look like a map lookup succeeded?
(defn div [a b] (if (zero? b) {:error "Div by 0"} (/ a b))
(let [res (div 42 0)]
(if (:error res)
...
...))
That's a very quick example of what it allows in Clojure. I find it generally useful to have different ways of querying keys that have different semantics.
I'm worried about people doing things like (:blacklisted? some-object) and missing a critical security check though if they look at the wrong part of a data structure. Also confusing to explain if get and :keyword semantics are noticeably different....