<< Back to previous view

[CLJ-907] records do not enforce type hints Created: 08/Jan/12  Updated: 01/Mar/13  Resolved: 27/Nov/12

Status: Closed
Project: Clojure
Component/s: None
Affects Version/s: Release 1.2, Release 1.3
Fix Version/s: None

Type: Defect Priority: Major
Reporter: Chip Salzenberg Assignee: Unassigned
Resolution: Declined Votes: 1
Labels: None


 Description   

Specify a record with a field as say ^String, but the constructor won't throw if you pass in a non-String.



 Comments   
Comment by Alan Malloy [ 10/Jan/12 4:59 PM ]

I doubt there's any desire to change this. Records only support typehints to enable performant storage of primitive types like ints, not to provide static typing in general. An Object is stored the same way as a String, so there's no need to pay attention to that typehint.

Functions behave the same way: ((fn [^String x] x) :test) works just fine. Only if you use a member method of the hinted type (such as .substring in this example) is an exception thrown then the compiler casts to String in order to call the method.

Comment by Chip Salzenberg [ 10/Jan/12 5:08 PM ]

I don't understand how you get from "this is how it is" to "this is how it is meant to be." The compiler can do a better job if the underlying field is properly typed: Type errors can be caught sooner, and cast operations can be omitted on use. This is a worthy enhancement.

Comment by Timothy Baldridge [ 27/Nov/12 1:52 PM ]

Alan is correct, the reason type hints exist is to reduce reflection and to allow for the unboxing of primitives. String is not a primitive and therefore type-hinting it only reduces the amount of reflection performed.

It has always been Rich's policy that any type systems used in Clojure should not throw compile time errors, but instead should only enhance the performance of existing code. (see the implementation of Typed Clojure for more info on this).

Closing this issue, as it is by design.

Comment by Timothy Baldridge [ 27/Nov/12 1:52 PM ]

By design.

Generated at Thu Oct 30 13:01:04 CDT 2014 using JIRA 4.4#649-r158309.