<< Back to previous view

[CTYP-118] Support clojure.core/cast Created: 13/Mar/14  Updated: 14/Mar/14  Resolved: 14/Mar/14

Status: Closed
Project: core.typed
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Enhancement Priority: Minor
Reporter: Ambrose Bonnaire-Sergeant Assignee: Ambrose Bonnaire-Sergeant
Resolution: Completed Votes: 0
Labels: None

Attachments: File ann-cast.diff     File ann-cast-v2.diff     File ann-cast-v3.diff     File ann-cast-v4.diff    

 Comments   
Comment by Di Xu [ 13/Mar/14 6:23 AM ]

annotate cast using add-invoke-special-method

Comment by Ambrose Bonnaire-Sergeant [ 13/Mar/14 6:36 AM ]

The invoke-special case should be more lenient.

If the first argument is a literal class, then it can be special cased, otherwise return :default.

Use RClass-of-with-unknown-params like in :instance-of.

Please add some tests in clojure.core.typed.test.core, testing the special case and the normal case.

Comment by Di Xu [ 13/Mar/14 6:56 AM ]

Well, I'm confused, I only aware of the literal class could be the first argument. could you give me some example of nonliteral class argument?

Comment by Ambrose Bonnaire-Sergeant [ 13/Mar/14 7:03 AM ]

I mean this is still type correct:

(ann foo [Class Any -> Any])
(defn foo [c a]
(cast c a))

Comment by Ambrose Bonnaire-Sergeant [ 13/Mar/14 9:15 AM ]

I wonder if `cast` should make an intersection of the original type and the casted type.

(cast String "a") => (I String (Value "a"))

Perhaps that's more useful. Just implement the simple RClass return type for now.

Instead of testing (-> args first :val), you should inspect the type of
(-> (check (first args)) expr-type ret-t c/fully-resolve-type).

If it's a Value type and the :val field is a `class?`, then it's a special case.

Comment by Di Xu [ 14/Mar/14 2:10 AM ]

thanks for the detailed solution. I'm still not very familiar with its internal type

Comment by Ambrose Bonnaire-Sergeant [ 14/Mar/14 2:53 AM ]

To make an intersection, use c/In.

Also need a test for (cast (ann-form Class Class) :any)

Make a failing test like this:

(is (u/top-level-error-thrown?
(cf (fn [] (cast "a" "a"))))

Please add a few failing tests.

Comment by Di Xu [ 14/Mar/14 3:39 AM ]

you mean (cast (ann-form Class Class) (class :any))?

Comment by Ambrose Bonnaire-Sergeant [ 14/Mar/14 4:07 AM ]

Ah yes.

Comment by Di Xu [ 14/Mar/14 5:43 AM ]

added

Comment by Ambrose Bonnaire-Sergeant [ 14/Mar/14 7:03 AM ]

Merged.

For future reference, throws-tc-error? can catch int-errors.

Thanks!

Comment by Di Xu [ 14/Mar/14 8:04 AM ]

got it.

Generated at Mon Dec 22 19:40:39 CST 2014 using JIRA 4.4#649-r158309.