Clojure

Add record? predicate

Details

  • Type: Enhancement Enhancement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: Release 1.6
  • Component/s: None
  • Labels:
  • Patch:
    Code and Test
  • Approval:
    Ok

Description

Would like a predicate that can be used to check whether an object is an instance of a defrecord.

Patch: clj-394-add-predicate-for-record-3.diff

Approach: Use existing marker interface IRecord to add new record? predicate.

user=> (defrecord Foo [x])
user.Foo
user=> (def f (->Foo 1))
#'user/f
user=> (record? f)
true

Screened by: Alex Miller

Activity

Hide
Assembla Importer added a comment -
Show
Assembla Importer added a comment - Converted from http://www.assembla.com/spaces/clojure/tickets/394
Rich Hickey made changes -
Field Original Value New Value
Fix Version/s Release 1.4 [ 10040 ]
Fix Version/s Backlog [ 10035 ]
Reporter Assembla Importer [ importer ]
Priority Blocker [ 1 ]
Rich Hickey made changes -
Priority Blocker [ 1 ] Minor [ 4 ]
Hide
Steve Miner added a comment -

As of Clojure 1.3, there are marker interfaces named clojure.lang.IType and clojure.lang.IRecord. You can use instance? with those interfaces. I'm not sure if they're actually documented for public use, but they seem to work as expected in 1.3 and 1.4. If you want record?, you can try this:

(defn record? [rec] (instance? clojure.lang.IRecord rec))

Show
Steve Miner added a comment - As of Clojure 1.3, there are marker interfaces named clojure.lang.IType and clojure.lang.IRecord. You can use instance? with those interfaces. I'm not sure if they're actually documented for public use, but they seem to work as expected in 1.3 and 1.4. If you want record?, you can try this: (defn record? [rec] (instance? clojure.lang.IRecord rec))
Christopher Redinger made changes -
Fix Version/s Release 1.4 [ 10040 ]
Fix Version/s Release 1.5 [ 10150 ]
Hide
Devin Walters added a comment -

See attached code and test. I'm unsure as to whether or not the location of the tests and predicates make sense. Please let me know if I should move them elsewhere.

Show
Devin Walters added a comment - See attached code and test. I'm unsure as to whether or not the location of the tests and predicates make sense. Please let me know if I should move them elsewhere.
Devin Walters made changes -
Patch Code and Test [ 10002 ]
Attachment clj-394-add-predicates-for-type-and-record.diff [ 11590 ]
Stuart Halloway made changes -
Fix Version/s Release 1.5 [ 10150 ]
Fix Version/s Release 1.6 [ 10157 ]
Alex Miller made changes -
Approval Vetted [ 10003 ]
Alex Miller made changes -
Description Sometimes one would like to know if an object is an instance of a deftype or a defrecord.
Add (possibly empty) marker Interfaces that allow an efficient test, plus test fns
'record?' and 'deftype?'.
Would like a predicate that can be used to check whether an object is an instance of a deftype or a defrecord.

*Patch:* {{clj-866-test-vars.patch}}

*Approach:* Use existing marker interfaces {{IRecord}} and {{IType}} to add new {{record?}} and {{type?}} predicates.
Alex Miller made changes -
Description Would like a predicate that can be used to check whether an object is an instance of a deftype or a defrecord.

*Patch:* {{clj-866-test-vars.patch}}

*Approach:* Use existing marker interfaces {{IRecord}} and {{IType}} to add new {{record?}} and {{type?}} predicates.
Would like a predicate that can be used to check whether an object is an instance of a deftype or a defrecord.

*Patch:* {{clj-394-add-predicates-for-type-and-record.diff}}

*Approach:* Use existing marker interfaces {{IRecord}} and {{IType}} to add new {{record?}} and {{type?}} predicates.
Alex Miller made changes -
Labels defrecord deftype
Hide
Alex Miller added a comment -

Tweaked patch to say added in 1.6, not 1.5.

Show
Alex Miller added a comment - Tweaked patch to say added in 1.6, not 1.5.
Alex Miller made changes -
Description Would like a predicate that can be used to check whether an object is an instance of a deftype or a defrecord.

*Patch:* {{clj-394-add-predicates-for-type-and-record.diff}}

*Approach:* Use existing marker interfaces {{IRecord}} and {{IType}} to add new {{record?}} and {{type?}} predicates.
Would like a predicate that can be used to check whether an object is an instance of a deftype or a defrecord.

*Patch:* {{clj-394-add-predicates-for-type-and-record-2.diff}}

*Approach:* Use existing marker interfaces {{IRecord}} and {{IType}} to add new {{record?}} and {{type?}} predicates.

{code}
user=> (defrecord Foo [x])
user.Foo
user=> (def f (->Foo 1))
#'user/f
user=> (record? f)
true
user=> (deftype T [])
user.T
user=> (def t (T.))
#'user/t
user=> (type? t)
true
{code}

*Screened by:* Alex Miller
Attachment clj-394-add-predicates-for-type-and-record-2.diff [ 12328 ]
Alex Miller made changes -
Approval Vetted [ 10003 ] Screened [ 10004 ]
Hide
Rich Hickey added a comment -

I think this should do record? only. type? conveys no semantics, and the things are not types themselves.

Show
Rich Hickey added a comment - I think this should do record? only. type? conveys no semantics, and the things are not types themselves.
Rich Hickey made changes -
Approval Screened [ 10004 ] Incomplete [ 10006 ]
Hide
Alex Miller added a comment -

Altering title and description per Rich's comment.

Show
Alex Miller added a comment - Altering title and description per Rich's comment.
Alex Miller made changes -
Description Would like a predicate that can be used to check whether an object is an instance of a deftype or a defrecord.

*Patch:* {{clj-394-add-predicates-for-type-and-record-2.diff}}

*Approach:* Use existing marker interfaces {{IRecord}} and {{IType}} to add new {{record?}} and {{type?}} predicates.

{code}
user=> (defrecord Foo [x])
user.Foo
user=> (def f (->Foo 1))
#'user/f
user=> (record? f)
true
user=> (deftype T [])
user.T
user=> (def t (T.))
#'user/t
user=> (type? t)
true
{code}

*Screened by:* Alex Miller
Would like a predicate that can be used to check whether an object is an instance of a defrecord.

*Patch:* {{clj-394-add-predicates-for-type-and-record-2.diff}}

*Approach:* Use existing marker interfaces {{IRecord}} to add new {{record?}} predicate.

{code}
user=> (defrecord Foo [x])
user.Foo
user=> (def f (->Foo 1))
#'user/f
user=> (record? f)
true
{code}

*Screened by:*
Summary Add marker Interfaces for defrecords and deftypes plus boolean test fns Add record? predicate
Hide
Alex Miller added a comment -

Added updated patch that just adds record?

Show
Alex Miller added a comment - Added updated patch that just adds record?
Alex Miller made changes -
Attachment clj-394-add-predicate-for-record-3.diff [ 12477 ]
Hide
Alex Miller added a comment -

Added new patch that contains only the record? fn and omits the type stuff. Marking Screened again.

Show
Alex Miller added a comment - Added new patch that contains only the record? fn and omits the type stuff. Marking Screened again.
Alex Miller made changes -
Approval Incomplete [ 10006 ] Screened [ 10004 ]
Description Would like a predicate that can be used to check whether an object is an instance of a defrecord.

*Patch:* {{clj-394-add-predicates-for-type-and-record-2.diff}}

*Approach:* Use existing marker interfaces {{IRecord}} to add new {{record?}} predicate.

{code}
user=> (defrecord Foo [x])
user.Foo
user=> (def f (->Foo 1))
#'user/f
user=> (record? f)
true
{code}

*Screened by:*
Would like a predicate that can be used to check whether an object is an instance of a defrecord.

*Patch:* {{clj-394-add-predicate-for-record-3.diff}}

*Approach:* Use existing marker interface {{IRecord}} to add new {{record?}} predicate.

{code}
user=> (defrecord Foo [x])
user.Foo
user=> (def f (->Foo 1))
#'user/f
user=> (record? f)
true
{code}

*Screened by:* Alex Miller
Attachment clj-394-add-predicate-for-record-3.diff [ 12478 ]
Labels defrecord deftype defrecord
Alex Miller made changes -
Attachment clj-394-add-predicate-for-record-3.diff [ 12477 ]
Rich Hickey made changes -
Approval Screened [ 10004 ] Ok [ 10007 ]
Stuart Halloway made changes -
Resolution Completed [ 1 ]
Status Open [ 1 ] Closed [ 6 ]

People

Vote (0)
Watch (4)

Dates

  • Created:
    Updated:
    Resolved: