convex icon indicating copy to clipboard operation
convex copied to clipboard

Allow keywords as functions on any type

Open helins opened this issue 4 years ago • 3 comments

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.

helins avatar Jun 29 '21 20:06 helins

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?

mikera avatar Jun 29 '21 22:06 mikera

(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.

helins avatar Jun 30 '21 06:06 helins

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....

mikera avatar Jun 30 '21 11:06 mikera