ClojureScript

Preserve type of function with metadata

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: 1.10.238, Next
  • Fix Version/s: None
  • Component/s: None
  • Labels:
  • Environment:
    N/A

Description

Currently, IWithMeta is implemented for functions by wrapping the function in a MetaFn type.

However, this breaks certain expectations about the typeof of the meta-fied value:

(defn Foo [_] "bar")

(goog/typeOf Foo)
;; => "function"

(goog/typeOf (with-meta Foo {:bar "baz"}))
;; => "object"

The primary case when this is not ideal is when interoping with JS code that does checks using typeof, like React does:

(react-is/isValidElementType Foo)
;; => true

(react-is/isValidElementType (with-meta Foo {:bar "baz"}))
;; => false

Ideally, with-meta would preserve the typeof value when possible.

Activity

Hide
William Acton added a comment -

There's a separate issue about MetaFn's and varargs, https://dev.clojure.org/jira/browse/CLJS-2446, whose proposed implementation also solves this particular edge case.

Show
William Acton added a comment - There's a separate issue about MetaFn's and varargs, https://dev.clojure.org/jira/browse/CLJS-2446, whose proposed implementation also solves this particular edge case.
Hide
William Acton added a comment -

Attached patch:

  • replaces `MetaFn` type with `meta-fn` helper that creates new fn
    with IMeta specify!'d on it.
  • Also fixes CLJS-2446: with-meta doesn't work for variable arguments functions
  • Adds tests for metadata and fns
Show
William Acton added a comment - Attached patch:
  • replaces `MetaFn` type with `meta-fn` helper that creates new fn with IMeta specify!'d on it.
  • Also fixes CLJS-2446: with-meta doesn't work for variable arguments functions
  • Adds tests for metadata and fns

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated: